簡體   English   中英

如何在沒有標題MVC的情況下獲取ViewBag.Title

[英]How to get ViewBag.Title without Title of Layout MVC

我在布局中將標題設置為:-

@ViewBag.Title | Web Clubbed

我的Index.html的Viewbag.Title為:-

@{
  ViewBag.Title = "Welcome to webclubbed web developement & online marketing"; 
}

現在,我在Index.cshtml中的標題顯示如下:-歡迎使用webclubbed網站開發和在線營銷| 網絡俱樂部

但是我只想從Index.cshtml中的標題中刪除“ | Web Clubbed”。 我怎樣才能做到這一點?

請幫忙!!

使用另一個viewbag值來存儲后綴的顯示/隱藏。

布局:

@{
  var suffix = " | Web Clubbed";
  var hideSuffix = ViewBag.HideSuffix as bool?;
  if (hideSuffix.HasValue && hideSuffix.Value){
    suffix = string.Empty;
  }
}

@ViewBag.Title @suffix

指數:

@{
  ViewBag.Title = "Welcome to webclubbed web developement & online marketing"; 
  ViewBag.HideSuffix = true;
}

僅在需要隱藏后綴時才需要設置ViewBag值。

您可以使用部分來執行此操作。

在您的_Layout頁面中:

<head>
    @if(isSectionDefined("TitleSection"))
    {
        RenderSection("TitleSection", false);
        //Setting required to false
    }
    else
    {
        <title>ViewBag.Title | Web Clubbed</title>
    }
</head>

在您的索引頁中:

@section TitleSection {
<title>ViewBag.Title</title>
}

您可以通過將RouteData值與視圖進行比較來實現此目的。

因此,在_Layout頁面中,您可以執行如下條件語句:

@ViewBag.Title = "Welcome to webclubbed web developement & online marketing";

@if(ViewContext.RouteData.Values["action"].ToString() == "Index")
{
    ViewBag.Title
}
else
{
    ViewBag.Title <span>|</span> Web Clubbed
}

現在,根據您擁有多少Index操作。您可能需要使該條件語句更加具體。

@if(ViewContext.RouteData.Values["action"].ToString() == "Index" && ViewContext.RouteData.Values["controller"].ToString() == "/*ControllerNameThatIndexIsIn*/")

讓我知道這是否有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM