Milligran
Milligran

Reputation: 3171

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?

Upvotes: 0

Views: 247

Answers (1)

ataravati
ataravati

Reputation: 9155

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

Upvotes: 1

Related Questions