如何避免需要一个视图模型为每个型号 [英] How to avoid needing a VIewModel for every Model

查看:103
本文介绍了如何避免需要一个视图模型为每个型号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET 4和MVC3。

I'm using ASP.NET 4 and MVC3.

通常情况下,我觉得我需要一个视图模型为我的模型显示信息。例如,采用以下模式

Often, I find that I need a ViewModel to display information for my Model. For example, take the following model

class Profile
{
   public int UserID { get; set; }
   public string Name { get; set; }
   public DateTime DOB { get; set; }
}

有是隐藏的用户名,而是显示用户名,因此常时间模式是类似于上面的一个要求,我必须拿出只是一个视图模型的用户名更改为用户名:

There is a requirement to hide the UserID, but to show the UserName, so often time for models that are similar to the one above, I have to come up with a ViewModel with just the UserID changed to UserName:

class ProfileViewModel
{
   public string UserName { get; set; }
   public string Name { get; set; }
   public DateTime DOB { get; set; }
}

有什么方法?

推荐答案

直到最近我总是通过我的模型,我的行动方法,因为我也认为具有相同的属性名称创建的ViewModels是重复(它不是)。这使我疼痛的很多。我现在已经再教育,几乎总是在我的操作方法独占使用的ViewModels(当然总是会有的情况下是很精细的模型直接传递给操作方法)。

Until recently I always passed my models to my action methods as I also thought that creating viewModels with the same property names was duplication (its not). This caused me a lot of pain. I have now been re-educated and almost always use viewModels exclusively in my action methods (of course there will always be situations were it is fine to pass the model directly to the action method).

读这个后这是我转换使用的ViewModels之一。这会告诉你以下内容:

Have a read of this post which is the one that converted me to using viewModels. This will tell you the following:


  1. 模型和的ViewModels之间的区别

  2. 当每一个应该被使用。

  3. 如何避免一些安全问题与默认模型粘合剂。

在的链接文章的信息,你也应该考虑的事情,如确认顶部。我有这样的实施 IValidateableObject 接口,以保证实体是一个有效的状态保存到数据库之前的模式。

On top of the information in the linked post you should also consider things such as validation. I had a model that implemented the IValidateableObject interface to ensure the entity was in a valid state before being saved to the database.

在我的ASP.NET应用程序中我想创造一个多步骤的形式,允许用户在多个页面中输入的信息。我在这里的问题是,ASP.NET也使用在模型绑定过程中的 IValidatableObject 接口。

In my ASP.NET application I wanted to create a multi-step form that allowed the user to enter the information over a number of pages. The problem I had here was that ASP.NET also uses the IValidatableObject interface during the model binding process.

如果你仅允许用户输入的为实体所需的信息的一个子集,该模型粘合剂将仅能够填补该给出的信息。根据您的验证的复杂程度,这可能导致在的ModelState 被标记为无效的的全部的实体是无效的。

If you are only allowing the user to enter a subset of the information required for the entity, the model binder will only be able to fill in the information that was given. Depending on how complex your validation is, this can result in the ModelState being marked as invalid as the entire entity is not valid.

我解决这个得到的方式是有一个视图模型重新presenting每每一步都有自己验证。这样你仅在确认在每一步的属性。一旦你到了最后一步,一切都有效,我创建一个使用用户提供的信息的适当实体。这个实体将只有在它执行数据库级别的验证检查(字段长度等)

The way I got around this was to have a viewModel representing each step each with its own validation. This way you are only validating the properties at each step. Once you get to the final step and everything is valid, I create an appropriate entity using the information given by the user. This entity will only have database-level validation checks performed upon it (field lengths etc.)

我的建议是不要回避的ViewModels而是要理解为什么他们的使​​用和拥抱他们。

My suggestion is not to avoid viewModels but to understand why they are used and embrace them.

这篇关于如何避免需要一个视图模型为每个型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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