DataType 属性不起作用

发布于 2024-11-29 21:09:48 字数 570 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑叹一世浮沉 2024-12-06 21:09:48

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)

李白 2024-12-06 21:09:48

包含:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

您是否在主页中

?另外,您的 Web.config 中还需要这些:

<appSettings>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

Did you include:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

in your master page?

Also, you need these in your Web.config:

<appSettings>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
じ违心 2024-12-06 21:09:48

我同意 DataType 属性不执行任何验证的事实。但是您已经设置了必需的属性,该属性应该可以工作。

您必须放置一个表单来呈现属性的控件。当表单发布后,单击“提交”。

假设您的表单已发布到以下方法。

[HttpPost]
        public ActionResult SaveRegisterDetails(Register registerDetails)
        {
            if (ModelState.IsValid)
                return View();
            else
                return View("Index", registerDetails);
        }

将您的 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.

[HttpPost]
        public ActionResult SaveRegisterDetails(Register registerDetails)
        {
            if (ModelState.IsValid)
                return View();
            else
                return View("Index", registerDetails);
        }

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.

二手情话 2024-12-06 21:09:48

对于 .NET Framework 4.5 及更高版本,有一个名为 [EmailAddress] 的验证属性,将其用于您的属性,如下所示

[Required]
[DataType(DataType.EmailAddress)]
[EmailAddress(ErrorMessageResourceName = "MustBeEmail", ErrorMessageResourceType = typeof(Resources))]
public string Email { get; set; }

另外不要忘记确保您在 web 中启用了客户端验证且不显眼.config 文件

<appSettings>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

并在剃须刀中包含 js 文件。

For .NET framework 4.5 and higher there is a validation attribute called [EmailAddress] use it for your property like this

[Required]
[DataType(DataType.EmailAddress)]
[EmailAddress(ErrorMessageResourceName = "MustBeEmail", ErrorMessageResourceType = typeof(Resources))]
public string Email { get; set; }

Also don't forget to make sure you enabled client side validation and unobtrusive in web.config file

<appSettings>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

and included js files in your razor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文