ViewModel中的最佳实践

时间:2022-12-30 20:06:44

I am wondering if it is a good idea or bad, placing things like a List of countries in ViewModel, for binding to a drop down list? For example on a site's Registration page.

我想知道这是一个好主意还是坏事,将诸如国家列表放在ViewModel中,用于绑定到下拉列表?例如,在网站的“注册”页面上。

I was under the impression that a ViewModel is supposed to represent an instance of the filled out form, but I think I may be wrong as I have seen other people put things like lists in their ViewModel.

我的印象是ViewModel应该代表填写表单的一个实例,但我认为我可能错了,因为我看到其他人在其ViewModel中放置了类似列表的内容。

Would it not be better to put it in a static class somewhere and called directly from the View?

将它放在某个静态类并直接从View中调用会不会更好?

Like CommonData.ListCountries(); and then using Lambda to convert to SelectList item list in the view Directly?

像CommonData.ListCountries();然后使用Lambda直接转换为视图中的SelectList项目列表?

2 个解决方案

#1


9  

As you've realized there are a variety of ways to accomplish your goal. While the MVC design pattern encourages certain application organizations how you organize your models, views and controllers is ultimately a matter of preference.

正如您所知,有多种方法可以实现您的目标。虽然MVC设计模式鼓励某些应用程序组织如何组织模型,但视图和控制器最终是一个偏好问题。

Scott Allen discusses his preference for dealing with ASP.NET MVC drop down lists in a blog post. Scott uses an extension method to convert an enumerable of a complex type into an IEnumerable<SelectListItem> on his model. He then describes that upon post back ASP.NET MVC will not be returning the IEnumerable<SelectListItem> he sent to the view, but only the value the user selected. He then suggests that utilizing two models can simplify things.

Scott Allen讨论了他在博客文章中处理ASP.NET MVC下拉列表的偏好。 Scott使用扩展方法将复杂类型的可枚举转换为其模型上的IEnumerable 。然后他描述了在回发后,ASP.NET MVC将不会返回他发送到视图的IEnumerable ,而只返回用户选择的值。然后他建议使用两个模型可以简化事情。

This is a reasonable description of what I refer to as ViewModels and FormModels. A ViewModel carries the display data to the view and a FormModel is used for carrying collected data back to a controller action. To explain further:

这是我称之为ViewModels和FormModels的合理描述。 ViewModel将显示数据携带到视图,FormModel用于将收集的数据传送回控制器动作。进一步解释:

  • ViewModels contain data that help render views. By organizing my ViewModels this way I can place all necessary information to render a particular view into an associated model. This prevents me from having to use ViewData for anything that's not truly temporary.
  • ViewModels包含有助于呈现视图的数据。通过以这种方式组织我的ViewModel,我可以放置所有必要的信息,以将特定视图呈现到关联的模型中。这使我不必将ViewData用于任何不是真正临时的事情。

  • FormModels are used to gather user input. FormModels (almost) never contain references to other complex types and are made up of primitives, DateTimes, and strings.
  • FormModels用于收集用户输入。 FormModels(几乎)从不包含对其他复杂类型的引用,它们由基元,DateTime和字符串组成。

In either case I have a hard rule to never reuse a model for a different view. Having your models closely aligned with the views used to render them makes your views easier to write. You don't have to worry about things like static methods because your models should be carrying data to their associated views in a form that is easy for them to render. Tools like AutoMapper can help "flatten" domain objects into models for display purposes.

在任何一种情况下,我都有一条硬规则,永远不会为不同的视图重用模型。让模型与用于渲染它们的视图紧密对齐,使您的视图更容易编写。您不必担心静态方法之类的问题,因为您的模型应该以易于呈现的形式将数据传递到其关联的视图。像AutoMapper这样的工具可以帮助将域对象“展平”到模型中以便显示。

For additional reading checkout: ASP.NET MVC terminology is tripping me up - why 'ViewModel'?

对于额外的阅读结帐:ASP.NET MVC术语让我沮丧 - 为什么'ViewModel'?

#2


3  

Whatever data your View needs, put it in the ViewModel.

无论View需要什么数据,都可以将其放在ViewModel中。

The way i see it, once your view is going through the rendering process, it should have all the info it needs from the Model it is bound to.

我看到它的方式,一旦你的视图通过渲染过程,它应该从它所绑定的模型中获得所需的所有信息。

If you start to use helper methods, then the View is "going back to the controller" in a sense. Extension/helper methods are fine for formatting, etc, but they should not call through the model.

如果你开始使用辅助方法,那么View在某种意义上是“回到控制器”。扩展/辅助方法适用于格式化等,但它们不应该通过模型调用。

Don't forget, you also have ViewData (basically HttpContext.Current.Items, lives for single request), which is a lightweight storage mechanism that can be used to share data across partial views (for example).

不要忘记,您还有ViewData(基本上是HttpContext.Current.Items,适用于单个请求),这是一种轻量级存储机制,可用于跨部分视图共享数据(例如)。

#1


9  

As you've realized there are a variety of ways to accomplish your goal. While the MVC design pattern encourages certain application organizations how you organize your models, views and controllers is ultimately a matter of preference.

正如您所知,有多种方法可以实现您的目标。虽然MVC设计模式鼓励某些应用程序组织如何组织模型,但视图和控制器最终是一个偏好问题。

Scott Allen discusses his preference for dealing with ASP.NET MVC drop down lists in a blog post. Scott uses an extension method to convert an enumerable of a complex type into an IEnumerable<SelectListItem> on his model. He then describes that upon post back ASP.NET MVC will not be returning the IEnumerable<SelectListItem> he sent to the view, but only the value the user selected. He then suggests that utilizing two models can simplify things.

Scott Allen讨论了他在博客文章中处理ASP.NET MVC下拉列表的偏好。 Scott使用扩展方法将复杂类型的可枚举转换为其模型上的IEnumerable 。然后他描述了在回发后,ASP.NET MVC将不会返回他发送到视图的IEnumerable ,而只返回用户选择的值。然后他建议使用两个模型可以简化事情。

This is a reasonable description of what I refer to as ViewModels and FormModels. A ViewModel carries the display data to the view and a FormModel is used for carrying collected data back to a controller action. To explain further:

这是我称之为ViewModels和FormModels的合理描述。 ViewModel将显示数据携带到视图,FormModel用于将收集的数据传送回控制器动作。进一步解释:

  • ViewModels contain data that help render views. By organizing my ViewModels this way I can place all necessary information to render a particular view into an associated model. This prevents me from having to use ViewData for anything that's not truly temporary.
  • ViewModels包含有助于呈现视图的数据。通过以这种方式组织我的ViewModel,我可以放置所有必要的信息,以将特定视图呈现到关联的模型中。这使我不必将ViewData用于任何不是真正临时的事情。

  • FormModels are used to gather user input. FormModels (almost) never contain references to other complex types and are made up of primitives, DateTimes, and strings.
  • FormModels用于收集用户输入。 FormModels(几乎)从不包含对其他复杂类型的引用,它们由基元,DateTime和字符串组成。

In either case I have a hard rule to never reuse a model for a different view. Having your models closely aligned with the views used to render them makes your views easier to write. You don't have to worry about things like static methods because your models should be carrying data to their associated views in a form that is easy for them to render. Tools like AutoMapper can help "flatten" domain objects into models for display purposes.

在任何一种情况下,我都有一条硬规则,永远不会为不同的视图重用模型。让模型与用于渲染它们的视图紧密对齐,使您的视图更容易编写。您不必担心静态方法之类的问题,因为您的模型应该以易于呈现的形式将数据传递到其关联的视图。像AutoMapper这样的工具可以帮助将域对象“展平”到模型中以便显示。

For additional reading checkout: ASP.NET MVC terminology is tripping me up - why 'ViewModel'?

对于额外的阅读结帐:ASP.NET MVC术语让我沮丧 - 为什么'ViewModel'?

#2


3  

Whatever data your View needs, put it in the ViewModel.

无论View需要什么数据,都可以将其放在ViewModel中。

The way i see it, once your view is going through the rendering process, it should have all the info it needs from the Model it is bound to.

我看到它的方式,一旦你的视图通过渲染过程,它应该从它所绑定的模型中获得所需的所有信息。

If you start to use helper methods, then the View is "going back to the controller" in a sense. Extension/helper methods are fine for formatting, etc, but they should not call through the model.

如果你开始使用辅助方法,那么View在某种意义上是“回到控制器”。扩展/辅助方法适用于格式化等,但它们不应该通过模型调用。

Don't forget, you also have ViewData (basically HttpContext.Current.Items, lives for single request), which is a lightweight storage mechanism that can be used to share data across partial views (for example).

不要忘记,您还有ViewData(基本上是HttpContext.Current.Items,适用于单个请求),这是一种轻量级存储机制,可用于跨部分视图共享数据(例如)。