Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC Client Side Validation
    primarykey
    data
    text
    <p>Learning MVC 3 and I am trying to use clientside validation in MVC using AJAX:</p> <p>I have included the suggestions in article <strong>Call MVC 3 Client Side Validation Manually for ajax posts</strong>, but this does not work for me, it still sees the form as valid. What am I missing??</p> <p>I have included the following in my aplication:</p> <p><strong>Root web.config file:</strong></p> <pre><code>&lt;appSettings&gt; &lt;add key="webpages:Version" value="1.0.0.0" /&gt; &lt;add key="ClientValidationEnabled" value="true" /&gt; &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt; &lt;/appSettings&gt; </code></pre> <p><strong>Layout page scripts:</strong></p> <pre><code>&lt;script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"&gt;&lt;/script&gt; </code></pre> <p><strong>My model:</strong></p> <pre><code>[Table("Person")] public class Person { [Key] public int PersonID { get; set; } [Column("FullName")] [Required(ErrorMessage = "Give me a name.")] public string NameFull { get; set; } [Required(ErrorMessage = "Give me an email.")] public string EmailAddress { get; set; } [Required(ErrorMessage = "Give me a mobile number.")] public string MobileNo { get; set; } [ForeignKey("Agency")] [Required(ErrorMessage = "Give me an agency.")] public int AgencyID { get; set; } public virtual Agency Agency { get; set; } } </code></pre> <p><strong>The method to do the ajax call (fires on an onclick event):</strong></p> <pre><code>function LoadPage(SFORM, SACTION, SMETHOD) { $('#divLoading').slideDown(1); var doc = document.getElementById(SFORM) var dataform = $(doc).serialize(); var $form = $(doc); if ($form.valid()) { //Ajax call here $.ajax({ type: SMETHOD, url: SACTION, data: dataform, complete: function () { $("#divLoading").slideUp(1, function () { FinishLoad() }); }, success: function (data) { $("#divMain").html(data) } }); } } </code></pre> <p><strong>The View (.cshtml):</strong></p> <pre><code>&lt;form id="frmCreate" name="frmCreate"&gt; @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Person&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.NameFull) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.NameFull) @Html.ValidationMessageFor(model =&gt; model.NameFull) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.EmailAddress) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.EmailAddress) @Html.ValidationMessageFor(model =&gt; model.EmailAddress) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.MobileNo) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.MobileNo) @Html.ValidationMessageFor(model =&gt; model.MobileNo) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.AgencyID, "Agency") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownList("AgencyID", String.Empty) @Html.ValidationMessageFor(model =&gt; model.AgencyID) &lt;/div&gt; &lt;p&gt; &lt;input type="button" value="Create" onclick="LoadMenuItem('frmCreate','@Url.Action("Create", "Person")', 'POST')" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload