I am building an MVC 3 web application. I want to use Data Annotations for my entity class and then use unobtrusive client-side validation before sending a message back to the server. This works fine when creating a regular message. I get validation and a check summary if any of the fields is invalid. However, I want to publish information through ajax and json. How can I manually check the form on the client side and then return the ajax message to the server. Below is a summary version of my code.
public class Customer { [Required(ErrorMessage = "The customer first name is required.")] public string FirstName { get; set; } [Required(ErrorMessage = "The customer last name is required.")] public string LastName { get; set; } } <% using (Html.BeginForm()) { %> <%: Html.LabelFor(model => model.FirstName, "First Name")%> <%: Html.TextBoxFor(model => model.FirstName, new { @class = "TextBox", id = "Customer.FirstName" })%> <%: Html.ValidationMessageFor(model => model.FirstName, "*")%> <%: Html.LabelFor(model => model.LastName, "Last Name")%> <%: Html.TextBoxFor(model => model.LastName, new { @class = "TextBox", id = "Customer.LastName" })%> <%: Html.ValidationMessageFor(model => model.LastName, "*")%> <div id="CustomerEditSave" class="Button CustomerEditButtons" style="margin-right:40px;"> <a href="#">Save</a> </div> <%: Html.ValidationSummary(true) %> <% } %>
I tried this code, but it only checks the first name and does not display a check summary.
$("#CustomerEditSave").click(function () { $(form).validate();
javascript jquery validation asp.net-mvc asp.net-mvc-3
Thomas Feb 26 2018-11-21T00: 00Z
source share