开发者

DataType attribute doesn't work

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开发者_如何学C; }
}

Thanks for replying.


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)


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>


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜