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

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

问题描述

对于 ViewBag,我听说它不能使用.我认为应该将 ViewBag 中的内容合并到视图模型中?

问题:

  1. 我的假设是否高于最佳实践.(不要使用 ViewBag,然后在视图模型中使用它)

  2. 是否存在绝对需要 ViewBag 的情况?

解决方案

ViewBag 是一个动态字典.因此,当使用 ViewBag 在操作方法和视图之间传输数据时,如果您在尝试访问视图中的 ViewBag 项时在代码中输入错误,则编译器将无法捕捉到.您的视图将在运行时崩溃 :(

通常,使用视图模型在操作方法和视图之间传输数据是个好主意.视图模型是一个简单的 POCO 类,它具有特定于视图的属性.所以如果你想传递一些额外的数据来查看,向你的视图模型添加一个新属性并使用它.强类型视图使代码更清晰,更易于维护.使用这种方法,您无需将 viewbag 字典项显式转换为某些类型,而这与查看包有关.

public class ProductsForCategoryVm{公共字符串类别名称 { 设置;获取;}公共列表<ProductVm>产品{设置;获取;}}公共类 ProductVm{公共 int Id {set;get;}公共字符串名称 { set;get;}}

在您的操作方法中,创建此视图模型的对象,加载属性并将其发送到视图.

public ActionResult Category(int id){var vm= new ProductsForCategoryVm();vm.CategoryName = "书籍";vm.Products=新列表{new ProductVm { Id=1, Name="The Pragmatic Programmer" },新的 ProductVm { Id=2, Name="Clean Code" }}返回视图(vm);}

还有你的视图,它是强类型的视图模型,

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

下拉数据?

很多教程/书籍都有使用 ViewBag 作为下拉数据的代码示例.我个人还是觉得 ViewBag 不应该用于此.它应该是视图模型中 List> 类型的属性,以传递下拉数据.这是一个帖子,其中包含有关如何执行此操作的示例代码.

<块引用>

是否存在绝对需要 ViewBag 的情况?

在一些有效的用例中,您可以(不必要)使用 ViewBag 发送数据.例如,你想在你的布局页面上显示一些东西,你可以使用 ViewBag.另一个例子是存在于默认 MVC 模板中的 ViewBag.Title (用于页面标题).

public ActionResult Create(){ViewBag.AnnouncementForEditors="小心";返回视图();}

并且在布局中,您可以阅读ViewBag.AnnouncementForEditors

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

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天全站免登陆