1

I am having trouble with client side validation when using Ajax and jQuery to handle a form sumission when using ASP.NET MVC 3.

Checking if the model is valid on the server side works, however the following snippet does not trigger the client side valiation.

Am I missing something?

@model ViewModels.LeadDetailModelCore @{using (Html.BeginForm("UpdateCore", "Leads", new { area = "Telesales" }, FormMethod.Post, new { id = "coreSave" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) @Html.HiddenFor(model => model.Lead_ID) @Html.LabelFor(model => model.LeadStatus_ID) @Html.DropDownListFor(model => model.LeadStatus_ID, new SelectList(Model.LeadStatuses, "LeadStatus_ID", "LeadStatus_Name"), "-- Please select a status --") @Html.ValidationMessageFor(model => model.LeadSource_ID) }} <script type="text/javascript"> // NOTE ADD $(function () { $('#coreSave').die().live("submit", function (e) { e.preventDefault(); var form = $(this); var val = form.validate() if (val.valid()) { $("#ProgressDialog").dialog("open"); // post via ajax } return false; }); }); </script> 

    1 Answer 1

    6

    Make sure that you have included the proper scripts:

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

    and that you manually parse validation for all dynamic DOM elements. So everytime you update the DOM you should register client validation if this form was not part of the DOM when you initially loaded the page:

    $.validator.unobtrusive.parse($('#coreSave')); 

    You might also find the following answer useful for using jQuery dialog with partial views.

    5
    • Many thanks for the reply. I have references to both of the jquery.validate scripts but I'm getting nothing when I call $.validator.unobtrusive.parse($('#coreSave'));, I'm getting Microsoft JScript runtime error: Unable to get value of the property 'parse': object is null or undefined
      – Nick
      CommentedOct 16, 2011 at 15:25
    • @Nick, have you enabled client validation in your web.config: <add key="ClientValidationEnabled" value="true"/>? Also if you are getting this error it means that you haven't referenced correctly the jquery.validate.unobtrusive.min.js script as it is there where this method is defined.CommentedOct 16, 2011 at 15:27
    • That's what I thought. The web.config value is in place and my script statements are <script src="@Url.Script("jquery-1.5.1.min.js")" type="text/javascript"></script> <script src="@Url.Script("jquery-ui-1.8.16.custom.min.js")" type="text/javascript"></script> <script src="@Url.Script("jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Script("jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
      – Nick
      CommentedOct 16, 2011 at 15:33
    • 1
      @Nick, what is Url.Script? There is no such method in the standard library. Look with FireBug that all scripts are properly served.CommentedOct 16, 2011 at 15:34
    • Thanks for the great help. The script was referenced twice by my asset manager and this was causing my problem.
      – Nick
      CommentedOct 16, 2011 at 15:41

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.