MVC ViewBag最佳实践 [英] MVC ViewBag Best Practice

查看:254
本文介绍了MVC ViewBag最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于ViewBag,我听说那是一个没有没有使用。
我假设有从ViewBag内容应纳入视图模型?

问:


  1. 是我的假设上面的最佳实践。 (不使用ViewBag,二来有它在视图模型)


  2. 是否有其中ViewBag是绝对必要的情况呢?



解决方案

ViewBag是一个动态的字典。因此,使用ViewBag传输的操作方法和视图之间传送数据时,编译器将无法赶上,如果你试图访问视图中的ViewBag项目时在code做一个印刷工。你的看法会崩溃在运行时:(

一般来说是使用视图模式,您的操作方法和视图之间传输数据是个好主意。视图模型是具有特定的视图属性一个简单的POCO类。所以,如果你想通过一些额外的数据查看,一个新的属性添加到您的视图模型,并使用that.Strongly类型的视图使code更清洁,更易于维护。使用这种方法,你不必做你viewbag词典项目的显式铸造某些类型的来回,你有看法袋做的。

 公共类ProductsForCategoryVm
{
  公共字符串类别名称{集;获取; }
  公开名单< ProductVm>产品{集;获取;}
}
公共类ProductVm
{
  公众诠释标识{集;获取;}
  公共字符串名称{集;获取;}
}

而在你的操作方法,创建这个视图模型的对象,加载的属性和发送到视图。

 公众的ActionResult类别(INT ID)
{
  VAR VM =新ProductsForCategoryVm();
  vm.CategoryName =书;
  vm.Products =新的List< ProductVm> {
     新ProductVm {ID = 1,名称=程序员修炼},
     新ProductVm {ID = 2,名称=清洁code}
  }
  返回查看(VM);
}

和你的观点,这是强类型的视图模型,

  @model ProductsForCategoryVm
< H2> @ Model.CategoryName< / H>
@foreach(在Model.Products VAR项)
{
    &所述p为H.; @ item.Name&下; / P>
}

下拉数据?

很多的教程/书籍有一个使用ViewBag为下拉数据code样品。我个人还是觉得ViewBag的不应该被用于此目的。它应该是类型列表℃的财产;在你的视图模型SelectListItem >通过下拉数据。这里是一个<一个href=\"http://stackoverflow.com/questions/12394399/mvc3-dropdown-list-from-array/12394715#12394715\">post就如何做到这一点的例子code。


  

是否有其中ViewBag是绝对必要的情况呢?


有一些有效的使用情况下,你可以(的没有必要的)使用ViewBag发送数据。例如,你想显示你的页面布局上的东西,你可以用ViewBag了点。另一个例子是 ViewBag.Title (用于页面标题的),在默认的MVC模板present。

 公众的ActionResult的Create()
{
   ViewBag.AnnouncementForEditors =小心
   返回查看();
}

和布局,你可以阅读 ViewBag.AnnouncementForEditors

 &LT;身体GT;
&LT; H1&GT; @ ViewBag.AnnouncementForEditors&LT; / H1&GT;
&LT; D​​IV CLASS =箱体内容&GT;
    @RenderBody()
&LT; / DIV&GT;
&LT; /身体GT;

For the ViewBag, I heard it was a no-no to use. I would assume have the content from the ViewBag should be incorporated into a view model?

Question:

  1. Is my assumption above the best practice. (Not to use a ViewBag and second to have it in the view model)

  2. Are there situations where a ViewBag is absolutely necessary?

解决方案

ViewBag is a dynamic dictionary. So when using ViewBag to transfer data between action methods and views, your compiler won't be able to catch if you make a typo in your code when trying to access the ViewBag item in your view. Your view will crash at run time :(

Generally it is a good idea to use a view model to transfer data between your action methods and views. view model is a simple POCO class which has properties specific to the view. So if you want to pass some additional data to view, Add a new property to your view model and use that.Strongly typed Views make the code cleaner and more easy to maintain. With this approach, you don't need to do explicit casting of your viewbag dictionary item to some types back and forth which you have to do with view bag.

public class ProductsForCategoryVm
{
  public string CategoryName { set;get; }
  public List<ProductVm> Products { set;get;}    
}
public class ProductVm
{
  public int Id {set;get;} 
  public string Name { set;get;}
}

And in your action method, create an object of this view model, load the properties and send that to the view.

public ActionResult Category(int id)
{
  var vm= new ProductsForCategoryVm();
  vm.CategoryName = "Books";
  vm.Products= new List<ProductVm> {
     new ProductVm { Id=1, Name="The Pragmatic Programmer" },
     new ProductVm { Id=2, Name="Clean Code" }
  }
  return View(vm);
}

And your view, which is strongly typed to the view model,

@model ProductsForCategoryVm
<h2>@Model.CategoryName</h2>
@foreach(var item in Model.Products)
{
    <p>@item.Name</p>
}

Dropdown data ?

A lot of tutorials/books has code samples which uses ViewBag for dropdown data. I personally still feel that ViewBag's should not be used for this. It should be a property of type List<SelectListItem> in your view model to pass the dropdown data. Here is a post with example code on how to do that.

Are there situations where a ViewBag is absolutely necessary?

There are some valid use cases where you can(not necessary) use ViewBag to send data. For example, you want to display something on your Layout page, you can use ViewBag for that. Another example is ViewBag.Title (for the page title) present in the default MVC template.

public ActionResult Create()
{
   ViewBag.AnnouncementForEditors="Be careful";
   return View();
}

And in the layout, you can read the ViewBag.AnnouncementForEditors

<body>
<h1>@ViewBag.AnnouncementForEditors</h1>
<div class="container body-content">
    @RenderBody()
</div>
</body>

这篇关于MVC ViewBag最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆