ASP.NET MVC 3中的ViewBag.Title与Page.Title

abe*_*nci 1 asp.net-mvc

为什么有时候@ViewBag.Title包含正确的页面标题而@Page.Title为null?我们正在调试我们的View/Layout代码,我们注意到了这种差异.

谢谢.

Ben*_*cke 8

当您使用asp.Net MVC时,您可以使用一些工具将数据传输到您的页面.

  1. 您发送到视图的模型.
  2. ViewBag.
  3. TempData的.
  4. 饼干.
  5. 会话.

每个人都有自己的用例我们将使用的示例是基本列表和更新视图对于公司集合

该模型

只要您将已定义的数据集发送到视图并且应包含页面的数据,并且通常模型特定于页面或控制器,就应该使用该模型.

ViewBag

只要您需要将常规数据发送到未在模型上定义的页面或在视图树中使用的数据,就应使用ViewBag .

临时数据

Temp Data是临时数据的名称存储,如果您不确定数据是否到达目的地,或者您希望在不添加参数的情况下在操作之间传递数据,则应使用此数据.

饼干

Cookie就像全局变量一样,是整个应用程序中的主机数据.

会议

像cookie这样的会话是全局变量,但是它们的生命周期会话的不同可以用于诸如在请求之间存储表单数据之类的东西不要使用session来执行此操作而是使用类似http://garlicjs.org/之类的东西.

让我们看一下示例我们有以下ViewModel

public class Company 
{
  public int? id { get; set; }
  public string Name { get; set; }
  public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

布局页面如下所示

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    //This looks for the Title property on the ViewBag and inserts it in the title tags
    <title>@ViewBag.Title</title>
</head>
<body>
   @Html.Action("Header", "PageElements")
   @RenderBody()
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

PageElements控制器上的Header Action看起来像这样

public PartialViewResult Header()
{
  ViewBag.CurrentLink = HttpContext.Current.Request.Cookies["CurrentLink"];

  return PartialView();
}
Run Code Online (Sandbox Code Playgroud)

Header Partial View看起来像这样

<header>
   <ul> 
      <li class="@(ViewBag.CurrentLink == "Home" ? "Current" : "")"> 
          @Html.ActionLink("Home", "Index", "Home")
      </li>
      <li  class="@(ViewBag.CurrentLink == "Companies" ? "Current" : "")"> 
          @Html.ActionLink("Companies", "Index", "Companies")
      </li>
   </ul>
</header>
Run Code Online (Sandbox Code Playgroud)

用于更新的控制器操作如下所示

public ViewResult Update(int id)
{  

  //Gets a company from the database
  var model = db.GetCompany(id);

  //Sets The Title property on the ViewBag to Update : CompanyName
  ViewBag.Title = String.Format("Update : {0}", model.Name);  

  //Sends the company to the view
  return View(model);
}
Run Code Online (Sandbox Code Playgroud)

更新视图如下所示

@model Application.Models.Company
@{
   Layout = "_layout.cshtml";
}

@using (Html.BeginForm())
{ 
   @Html.HiddenFor(model => Model.id)

   @Html.LabelFor(model => Model.Name)
   @Html.TextBoxFor(model => Model.Name)

   @Html.LabelFor(model => Model.Description)
   @Html.TextAreaFor(model => Model.Description)
   <button>Submit</button>
}
Run Code Online (Sandbox Code Playgroud)

Post Action for Update看起来像这样

[HttpPost]
public ActionResult Update(Company model)
{ 
  //Attempts to update the model 
  if (model.Update())
  {
     //Set the message to update succeeded
     TempData["Message"] = String.Format("{0} Successfully updated");
  }
  else   
  {
     //Set the message to update failed
     TempData["Message"] = String.Format("{0} filed to update");
  } 

  return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)

控制器对公司的行动看起来像这样

public ViewResult Index()
{
  //Index is the entrypoint for companies so we set the currentlink to companies while we are in companies
  HttpContext.Current.Request.Cookies["CurrentLink"] = "Companies";      

  //Gets the success or failure message from the temp data
  ViewBag.Message = TempData["Message"]; 

  //Sets The Title property on the ViewBag to List of companies
  ViewBag.Title = "List of companies";

  var model = db.GetAllCompanies();

  return View(model);
}
Run Code Online (Sandbox Code Playgroud)

列表视图如下所示

@model IEnumrable<Application.Models.Company>
@{
   Layout = "_layout.cshtml";
}
<span>@ViewBag.Message</span>
<ul>
@foreach (var company in Model) 
{
  <li>@company.Name : @company.Description @Html.AnchorLink("Edit", "Update", new { company.id}) </li>
}
</ul>
Run Code Online (Sandbox Code Playgroud)

让我们讨论一下这个应用程序的流程如何工作

  1. 指数行动被公司触发.
  2. 我们将当前链接设置为Cookie中的公司
  3. 然后我们在ViewBag中设置Title
  4. 我们检查TempData中的消息并将它们放入ViewBag中
  5. 然后我们将公司集合放入模型并将其发送到页面.
  6. 现在剃刀开始并开始首先渲染页面
  7. 然后布局呈现并从ViewBag获取Title值
  8. 然后标题局部视图呈现并从Cookie获取currentlink值
  9. 我们点击公司的更新按钮
  10. 更新操作将运行
  11. 从数据库获取公司并将其发送到模型中的视图
  12. 我们在ViewBag中将标题更改为更新
  13. 页面首先呈现并将公司数据放在表单上,​​从模型中
  14. 然后布局呈现并从ViewBag获取标题值
  15. 然后标题呈现并从cookie获取currentlink (值仍然是公司,我们没有必要更改它)
  16. 然后我们提交表单并向TempData添加一条消息
  17. 现在我们回到公司列表中,我们可以从TempData获取成功或失败消息

我们使用Model特定数据发送到视图.我们使用ViewBagGeneral数据发送到视图和布局.我们使用TempData不使用参数的情况下在操作之间传递数据.我们使用Cookie来存储暂时不会改变的数据.