DataType 属性不起作用
在 ASP.NET MVC 3 Web 应用程序中,我有一个视图模型,其属性标记为 DataType 属性,但它们不在客户端和服务器端进行实际验证,为什么?
public class RegisterModel
{
[Required(ErrorMessage = "Phone number is required")]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone number")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage = "E-mail address is required")]
[DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid date (ex: 2/14/2011)")]
[Display(Name = "E-mail address")]
public string Email { get; set; }
}
感谢您的回复。
In ASP.NET MVC 3 web application I have a viewmodel with properties which marked with DataType attributes, but they don't do actual validation on cliant side, and on server side, Why?
public class RegisterModel
{
[Required(ErrorMessage = "Phone number is required")]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone number")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage = "E-mail address is required")]
[DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid date (ex: 2/14/2011)")]
[Display(Name = "E-mail address")]
public string Email { get; set; }
}
Thanks for replying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
DataType 属性不能用于验证用户输入。它们仅提供使用模板化助手呈现值的提示。
如果没有满足您需要的内置验证属性(例如“Range”或“Required”),那么您应该做的是创建一个自定义属性验证属性,并使用该属性来装饰您的模型属性以进行验证。 EG,用于 DataType.EmailAddress
这在 Pro Asp.net mvc 3 Framework(Adam Freeman 和 Steve Sanderson,第 618 页或附近)中进行了描述
DataType attributes can't be used to validate user input. They only provide hints for rendering values using templated helpers.
If there is not a built in validation attribute for what you need, eg, Range or Required, then what you should do is to create a custom property validation attribute and decorate your model property with that for the purposes of validation. EG, for DataType.EmailAddress
This is described in Pro Asp.net mvc 3 Framework (Adam Freeman and Steve Sanderson, page 618 or thereabouts)
包含:
您是否在主页中
?另外,您的
Web.config
中还需要这些:Did you include:
in your master page?
Also, you need these in your
Web.config
:我同意 DataType 属性不执行任何验证的事实。但是您已经设置了必需的属性,该属性应该可以工作。
您必须放置一个表单来呈现属性的控件。当表单发布后,单击“提交”。
假设您的表单已发布到以下方法。
将您的 ViewModel 对象作为参数传递。从表单发布的所有值都将出现在该对象中。然后检查 ModelState 是否有效。如果无效,则返回发布表单的相同视图,并将 ViewModel 对象作为参数传递。
如果您使用的是 EditorForModel 或 DisplyForModel,请使用 ValidationSummary 帮助程序在 UI 上显示错误消息。
I agree with the fact that DataType attribute does not perform any validations. But you have Required attribute in place which should work.
You must be having a form placed to render the controls for your properties. When the form is posted on the submit click.
Suppose your form is posted to below method.
Pass your ViewModel object as parameter. All the values posted from form will be present in this object. Then check for ModelState, whether it is valid or not. If it is not valid return the same view from where form was posted and pass ViewModel object as parameter.
If you are using EditorForModel or DisplyForModel, then use ValidationSummary helper to show error messages on UI.
对于 .NET Framework 4.5 及更高版本,有一个名为
[EmailAddress]
的验证属性,将其用于您的属性,如下所示另外不要忘记确保您在
web 中启用了客户端验证且不显眼.config
文件并在剃须刀中包含
js
文件。For .NET framework 4.5 and higher there is a validation attribute called
[EmailAddress]
use it for your property like thisAlso don't forget to make sure you enabled client side validation and unobtrusive in
web.config
fileand included
js
files in your razor.