简体   繁体   中英

Forms validation System.ComponentModel.DataAnnotations and JQuery submit

I have a C# asp.net MVC web application, and I'm using System.ComponentModel.DataAnnotations for form validation.

Here is an example of validation on a password field:

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; 

In my View, the form has:

@using (Html.BeginForm() {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

I'm planning to use JQuery to Submit my form/model.

Example:

$.post("@Url.Action("Update")",values,function(data)
{
    // do stuff;
});

My question is: will the built-in forms validation still occur, even though I am submitting the data with JQuery?

Yes, it will. The validation in asp.net MVC is done as part of Model Binding. So, when you post your form data to the Update Action, the validation is done when the data is being bound to your Model.

You can read more about it here: Validating Model Data in an MVC Application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM