Viewbag.title像“String Title”+@model.name 是可能的

本文关键字:name +@model title String Title Viewbag | 更新日期: 2023-09-27 17:56:08

我想让页面标题像"索引-'类别名称'"。我尝试使用这样的代码:

@model PagedList.IPagedList<Article>
@using PagedList.Mvc;
@using PagedList;
@{
    ViewBag.Title = "Index"+@Model.Category.Name;
}

但它没有用。如何使页面标题像"索引-C#"?我应该使用什么?

Viewbag.title像“String Title”+@model.name 是可能的

你应该

引用你的模型,只是Model,而不是@Model。当您处于 HTML 标记的中间时,使用 @ 字符是为了开始编写代码,但在这种情况下,您已经在代码块中(在上一行中以@{开头)。

尝试:

 ViewBag.Title = "Index" + Model.Category.Name;

就像这样:

ViewBag.Title = "Index" + Model.Category.Name;

没有 @。

以下是一些链接,可帮助您了解何时/何地使用 @:

http://weblogs.asp.net/scottgu/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax

http://odetocode.com/blogs/scott/archive/2013/01/09/ten-tricks-for-razor-views.aspx