asp.net-mvc Razor 中的 ViewBag.Title 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24354004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is ViewBag.Title in Razor?
提问by Yoda
What is ViewBag.Titlein ASP.NET MVC 4?
什么是ViewBag.Title在ASP.NET MVC 4?
I have that View file:
我有那个视图文件:
@model IEnumerable<MvcMusicStore.Models.Genre>
@{
ViewBag.Title = "Store";
}
<h2>Index</h2>
and I do not know what changing ViewBag.Titlemay accomplish.
我不知道改变ViewBag.Title会带来什么。
回答by Justin Helgerson
From the ASP.NET site:
ViewBag is a dynamic object, which means you can put whatever you want in to it; the ViewBag object has no defined properties until you put something inside it.
ViewBag 是一个动态对象,这意味着你可以把任何你想要的东西放进去;ViewBag 对象没有定义的属性,直到您将某些内容放入其中。
The ViewBag.Titleproperty is simply a string object. In this case it's being used in the view to actually definethe Titleproperty. If you were to look in your _Layout.cshtmlfile you would likely see something like:
该ViewBag.Title属性只是一个字符串对象。在这种情况下,它在视图中被用于实际定义的Title属性。如果您要查看您的_Layout.cshtml文件,您可能会看到如下内容:
<title>@ViewBag.Title</title>
Remember when the property was defined in the view? When the page is finally rendered, that property ends up in the HTML markup looking like:
还记得属性是在视图中定义的吗?当页面最终呈现时,该属性最终出现在 HTML 标记中,如下所示:
<title>Store</title>
Which sets the browser title.
它设置浏览器标题。
回答by CodingYoshi
ViewBagis a dynamic object so you can define any property on it. In this case @ViewBag.Titleis assigned a string. In the _Layout.cshtmlpage you will see this line of code:
ViewBag是一个动态对象,因此您可以在其上定义任何属性。在这种情况下@ViewBag.Title被分配一个字符串。在_Layout.cshtml页面中,您将看到这行代码:
<title>@ViewBag.Title</title>
Any view which is using the _Layout.cshtmlas the layout will have a line of code similar to below:
任何将_Layout.cshtml用作布局的视图都会有一行类似于下面的代码:
@{
// This is setting the property to Hahaha
ViewBag.Title = "Hahaha";
}
If you did that, this is eventually what you will accomplish (see the red circle) showing the title of the page in the browser:
如果你这样做了,这就是你最终将完成的(见红色圆圈)在浏览器中显示页面的标题:
In one of your comments you ask:
在您的评论之一中,您会问:
Does whatever I want means also to put an complex object in it? I know that this suppose to be done by passing object to the model, but is this doable?
我想要的东西是否也意味着将一个复杂的对象放入其中?我知道这假设是通过将对象传递给模型来完成的,但这可行吗?
In other words you are asking if you can put a complex object in Titleproperty? Yes, you can do this but keep in mind to change the code in the _Layout.cshtmlfile, otherwise it will just call ToString()on your complex object and print the result in the browser title. But why would you do this? I do not suggest it. But yes you can put anything in it because it is dynamic but then you have to use it properly. You can even create other properties like this:
换句话说,您是在问是否可以将复杂的对象放入Title财产中?是的,您可以这样做,但请记住更改_Layout.cshtml文件中的代码,否则它只会调用ToString()您的复杂对象并将结果打印在浏览器标题中。但你为什么要这样做?我不建议这样做。但是是的,您可以在其中放入任何内容,因为它是动态的,但是您必须正确使用它。您甚至可以像这样创建其他属性:
ViewBag.SomeOtherProperty = new MyClass() {...};
However, passing a model is much better because it gives you compiler support and you will also get intellisence.
但是,传递模型要好得多,因为它为您提供编译器支持,并且您还将获得智能。


