验证在ASP.NET MVC的ViewModels [英] Validation on ViewModels in ASP.NET MVC

查看:83
本文介绍了验证在ASP.NET MVC的ViewModels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大部分如何在ASP.NET MVC实施验证的提示似乎围绕着该模型(模型和控制器或验证特性模型的装饰性能之间建立服务层)。

Most of the tips on how to implement validation in ASP.NET MVC seem to center around the Model (either building service layers between model and controller or decorating properties of the model with validation attributes).

在我的应用程序使用的ViewModels的控制器和视图之间的所有通信。

In my application I use ViewModels for all communication between the controllers and the views.

我有一个视图模型为我的所谓'LoginViewModel有一个名为EmailAddress的属性登录页面。

I have a ViewModel for my login page called 'LoginViewModel' with a property called 'EmailAddress'.

当用户进入他们的电子邮件地址和点击提交,此视图模型填充和发送到控制器,其中,电子邮件地址被确认。

When the user enters their email address and clicks submit, this ViewModel is populated and sent to the controller, where the email address is validated.

它必须是一个有效的电子邮件地址,用户必须从一个与系统中注册域名。

It must be a valid email address, and the user must be from a domain that's registered with the system.

什么是添加验证这一便捷的方式?我应该把在视图模型本身的验证?或者应该在控制器留?

What would be a convenient way to add validation to this? Should I put the validation in the ViewModel itself? Or should it stay in the controller?

推荐答案

我应该把验证在视图模型本身呢,还是应该把它留在控制器我与罗伯特同意,但我想补充一个插件更多的自动化。

"Should I put the validation in the ViewModel itself? Or should it stay in the controller" I agree with Robert but I would add a plug for additional automation.

如果你看一个工具,如的 XVAL ,你可以看到,常规验证(例如,需要的领域,范围内的数字,字符串匹配的正前pressions)可以通过自动装饰你的数据类的领域进行。事实上,XVAL可以使其进行客户端自动写例程验证中的JavaScript。无需编写任何code。更深层次的验证(例如是该用户在我们的数据库中注册一个域的成员?)发生服务器端模型层本身中。

If you look at a tool such as xVal, you can see that routine validation (e.g., required fields, numbers within ranges, strings matching regular expressions) can be automatically done by decorating fields of your data classes. In fact, xVal can automatically write the JavaScript for routine validations so that it is carried out client side. All without writing any code. Deeper validations (e.g. is this user a member of a domain registered in our database?) happen server-side inside the model layer itself.

使用视图模型成语可能对这个计划的一些挑战。我目前的做法是嵌入我的视图模型里面我的实体对象,例如

Using the ViewModel idiom can pose some challenges to this scheme. My current approach is to embed my entity objects inside my view model, e.g.

public class Contact {
	[Required]
	string Name { get; set; }
}

public class ContactView {
	public Contact Contact { get; set; }
	public string SomeOtherViewProperty { get; set; }
}

,然后在控制器,浅验证更新模型时发生了:

and then in the controller, shallow validation happens when updating the model:

UpdateModel(contactViewModel.Contact, "Contact");

和需要了解更多信息或更复杂的计算中发生的验证模型层本身中。

and the validations requiring more information or more complicated calculations happen inside the model layer itself.

另一种方法是不嵌入实体对象,但只是映射两者之间的各个字段。我最近意识到了一个名为 AutoMapper 工具自动链接域和视图模型对象之间的领域。它看起来像它应该支持这种验证方法,虽然我还没有使用它。

Another approach is not to embed the entity object but just map individual fields between the two. I've recently become aware of a tool called AutoMapper which automatically links fields between domain and view model objects. It looks like it should support this validation method, though I haven't used it yet.

这篇关于验证在ASP.NET MVC的ViewModels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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