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

时间:2013-03-04 16:30:27

标签: asp.net-mvc

为什么有时@ViewBag.Title包含正确的页面标题而@Page.Title为空?我们正在调试我们的视图/布局代码,我们注意到了这种差异。

感谢。

2 个答案:

答案 0 :(得分:8)

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

  1. 您发送给视图的模型。
  2. ViewBag。
  3. TempData的。
  4. 缓存数据。
  5. 会话。
  6. 每个人都有自己的用例 我们将使用的示例是基本的 列出并更新视图 对于公司集合

    模型

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

    ViewBag

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

    临时数据

    临时数据的名称表示临时数据的存储空间如果您不确定数据是否会到达目的地,或者您希望在操作之间传递数据不添加参数

    <强>缓存

    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; }
    }
    

    布局页面如下所示

    <!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>
    

    PageElements Contoller上的页眉操作如下所示

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

    标题部分视图如下所示

    <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>
    

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

    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);
    }
    

    更新视图如下所示

    @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>
    }
    

    更新后发布操作如下所示

    [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");
    }
    

    公司的控制器操作如下所示

    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);
    }
    

    列表视图如下所示

    @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>
    

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

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

      我们使用模型特定数据发送到视图。 我们使用 ViewBag 常规数据发送到视图和布局。 我们使用 TempData 在操作之间传递数据,而不使用参数。 我们使用 Cookie 来存储数据,这些数据暂时不会改变

答案 1 :(得分:4)

正确使用ViewBag.Title,因为它位于ViewBag的{​​{1}}属性的上下文中,而View来自asp.net webforms。这就是为什么Page.Title为空,没有上下文的原因。