ViewBag的真正含义是什么?

时间:2022-09-07 00:27:22

In an ideal 'hello world' one should pass a strongly typed model back to the view.

在理想的“hello world”中,应该将强类型模型传递回视图。

return View(MyModel);

If things get sticky, we can create a

如果事情变得棘手,我们可以创建一个

ViewModel

return View(MyViewModel);

ex.

MyViewModel

  1. MyModel
  2. Foo

I can avoid creating the whole ViewModel

我可以避免创建整个ViewModel

and use a ViewBag in part.

并部分使用ViewBag。

ViewBag.Foo = Foo;
return View(MyModel);

I realize I will lose some strongly typed functionality

我意识到我会丢失一些强类型的功能

(ex. Intellisense)

in my View for that ViewBag.

在我的ViewBag视图中。

Is this approach sloppy? against what MVC stands for?

这种做法是不是很草率?反对MVC代表什么?

If so, what is really the point of ViewBag?

如果是这样,ViewBag真正的意义何在?

3 个解决方案

#1


5  

The most useful use-case that I know of is out-of-band data like a message that might be shown on every page. An action filter could add that message to the ViewBag conditionally. You probably don't want to modify every view model class to hold that unrelated message because it might be a cross-cutting concern. An example of such a message/widget would be Stack Overflows outage announcements.

我所知道的最有用的用例是带外数据,例如可能在每个页面上显示的消息。动作过滤器可以有条件地将该消息添加到ViewBag。您可能不希望修改每个视图模型类以保存该无关消息,因为它可能是一个跨领域的问题。这种消息/小部件的一个例子是Stack Overflows中断公告。

I do not recommend using ViewBag instead of a view model. The model class approach has the typical static typing advantages at the small cost of writing a class.

我不建议使用ViewBag而不是视图模型。模型类方法具有典型的静态类型优势,而且编写类的成本很低。

#2


1  

This is a good question, personally, I don't really think there is any point - all it does is encourage "lazy" coding. dynamic objects have their uses, however, I don't see the ViewBag as being one of them, I would much rather use ViewData[]/ViewModel and get my type-safety in there, especially when I know what types I am dealing with.

这是一个很好的问题,就个人而言,我并不认为有任何意义 - 它所做的只是鼓励“懒惰”编码。动态对象有它们的用途,但是,我没有看到ViewBag是其中之一,我宁愿使用ViewData [] / ViewModel并在那里获得我的类型安全性,特别是当我知道我正在处理的类型时。

#3


1  

I haven't encountered a scenario where it was beneficial to use ViewBag over the alternative (a correctly structured view model). Some of the other answers mention using it for a piece of data that needs to be displayed on every page. In this scenario you should create a base ViewModel class that all of your ViewModels inherit from. I use this structure to store the logged in user, etc.

我没有遇到过将ViewBag用于替代方案(正确结构化的视图模型)是有益的。其他一些答案提到将它用于需要在每个页面上显示的数据。在这种情况下,您应该创建一个所有ViewModel继承的基本ViewModel类。我使用这种结构来存储登录用户等。

In my opinion ViewBag serves one purpose: to make it easy and quick to give a product demo of ASP.NET MVC while writing minimal code. It is not the best way to structure code, but it sells the product.

在我看来,ViewBag有一个目的:在编写最少的代码时,轻松快速地提供ASP.NET MVC的产品演示。它不是构建代码的最佳方式,但它销售产品。

#1


5  

The most useful use-case that I know of is out-of-band data like a message that might be shown on every page. An action filter could add that message to the ViewBag conditionally. You probably don't want to modify every view model class to hold that unrelated message because it might be a cross-cutting concern. An example of such a message/widget would be Stack Overflows outage announcements.

我所知道的最有用的用例是带外数据,例如可能在每个页面上显示的消息。动作过滤器可以有条件地将该消息添加到ViewBag。您可能不希望修改每个视图模型类以保存该无关消息,因为它可能是一个跨领域的问题。这种消息/小部件的一个例子是Stack Overflows中断公告。

I do not recommend using ViewBag instead of a view model. The model class approach has the typical static typing advantages at the small cost of writing a class.

我不建议使用ViewBag而不是视图模型。模型类方法具有典型的静态类型优势,而且编写类的成本很低。

#2


1  

This is a good question, personally, I don't really think there is any point - all it does is encourage "lazy" coding. dynamic objects have their uses, however, I don't see the ViewBag as being one of them, I would much rather use ViewData[]/ViewModel and get my type-safety in there, especially when I know what types I am dealing with.

这是一个很好的问题,就个人而言,我并不认为有任何意义 - 它所做的只是鼓励“懒惰”编码。动态对象有它们的用途,但是,我没有看到ViewBag是其中之一,我宁愿使用ViewData [] / ViewModel并在那里获得我的类型安全性,特别是当我知道我正在处理的类型时。

#3


1  

I haven't encountered a scenario where it was beneficial to use ViewBag over the alternative (a correctly structured view model). Some of the other answers mention using it for a piece of data that needs to be displayed on every page. In this scenario you should create a base ViewModel class that all of your ViewModels inherit from. I use this structure to store the logged in user, etc.

我没有遇到过将ViewBag用于替代方案(正确结构化的视图模型)是有益的。其他一些答案提到将它用于需要在每个页面上显示的数据。在这种情况下,您应该创建一个所有ViewModel继承的基本ViewModel类。我使用这种结构来存储登录用户等。

In my opinion ViewBag serves one purpose: to make it easy and quick to give a product demo of ASP.NET MVC while writing minimal code. It is not the best way to structure code, but it sells the product.

在我看来,ViewBag有一个目的:在编写最少的代码时,轻松快速地提供ASP.NET MVC的产品演示。它不是构建代码的最佳方式,但它销售产品。