简体   繁体   English

将ViewBag设置设置为.NET Razor页面的确切含义是什么?

[英]What is the exact meaning of this ViewBag setting into a .NET Razor page?

I am very new in .NET (I came from Java and Spring framework) and I have the following doubt about Razor pages. 我在.NET中非常新(我来自Java和Spring框架),并且对Razor页面有以下疑问。

I have a very simple page like this: 我有一个非常简单的页面,像这样:

@model Vidly.Models.Customer

@{
    ViewBag.Title = Model.Name;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Model.Name</h2>

the first line declare the model object used by this page having type Vidly.Models.Customer (it contains the data used into the view), infact I access and show the value of the Vidly.Models.Customer.Name property by: 第一行声明此页面使用的模型对象,其类型为Vidly.Models.Customer (它包含视图中使用的数据),实际上我访问并通过以下方式显示Vidly.Models.Customer.Name属性的值:

<h2>@Model.Name</h2>

My doubt is related to this section of the previous code: 我的疑问与先前代码的这一部分有关:

@{
    ViewBag.Title = Model.Name;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

What is it exactly doing? 它到底在做什么?

I think that it is declaring some kind of object containing 2 properties that represent something like the settings of this view page (correct me if I am doing wrong assertion). 我认为这是在声明某种包含2个属性的对象,这些属性表示类似此视图页面的设置(如果我做的是错误的声明,请纠正我)。

By this line it is declaring what is the page layout: 通过这一行,它声明什么是页面布局:

Layout = "~/Views/Shared/_Layout.cshtml";

But what exactly means this line: 但是这行到底是什么意思:

ViewBag.Title = Model.Name;

What is the ViewBag ? 什么是ViewBag and what means this setting? 这是什么意思? Model.Name should contain the value of the Name property inside the passed model object. Model.Name应该在传递的模型对象中包含Name属性的值。 What is exactly doing? 到底在做什么?

In general it is used for passing small amounts of data among controllers and views. 通常,它用于在控制器和视图之间传递少量数据。

In the above example is it most likely setting the title of the page which is usually in the _Layout.cshtml 在上面的示例中,最有可能设置页面的title ,该title通常位于_Layout.cshtml

<title>My Page - @ViewBag.Title</title> 

In ASP.NET MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller to view. 在ASP.NET MVC中,有三种方法-ViewData,ViewBag和TempData将数据从控制器传递到视图。

What is the ViewBag? 什么是ViewBag?

  • ViewBag can be used to transfer data from the controller to the view, mostly temporary data. ViewBag可用于将数据从控制器传输到视图,主要是临时数据。
  • The ViewBag's exist only with the http request. ViewBag仅与http请求一起存在。 It will be destroyed on redirection. 它将在重定向时销毁。
  • ViewBag is a property of ControllerBase class. ViewBag是ControllerBase类的属性。

What exactly means this line: 这行的确切含义是:

ViewBag.Title = Model.Name;

ViewBag can be more convenient to work with, since it doesn't require casting. ViewBag不需要铸造,因此使用起来更方便。 You are setting the title using ViewBag. 您正在使用ViewBag设置标题。 Since the ViewBag derives from DynamicViewData, this means that can allows the creation of dynamic properties using dot notation @ViewBag.SomeKey = <value or object> . 由于ViewBag是从DynamicViewData派生的,因此这意味着可以使用点表示法@ViewBag.SomeKey = <value or object>创建动态属性。

Note: ViewBag isn't available in Razor Pages. 注意:ViewBag在Razor Pages中不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM