Note that there are some explanatory texts on larger screens.

plurals
  1. POUse ASP.NET MVC validation with jquery ajax?
    primarykey
    data
    text
    <p>I have simple ASP.NET MVC action like this : </p> <pre><code>public ActionResult Edit(EditPostViewModel data) { } </code></pre> <p>The <code>EditPostViewModel</code> have validation attributes like this : </p> <pre><code>[Display(Name = "...", Description = "...")] [StringLength(100, MinimumLength = 3, ErrorMessage = "...")] [Required()] public string Title { get; set; } </code></pre> <p>In the view I am using the following helpers : </p> <pre><code> @Html.LabelFor(Model =&gt; Model.EditPostViewModel.Title, true) @Html.TextBoxFor(Model =&gt; Model.EditPostViewModel.Title, new { @class = "tb1", @Style = "width:400px;" }) </code></pre> <p>If I do a submit on a form that this textbox is placed in a validation will be done first on client and then on service(<code>ModelState.IsValid</code>).</p> <p>Now I got a couple of questions :</p> <ol> <li><p>Can this be used with jQuery ajax submit instead? What I am doing is simply remove the form and on clicking the submit button a javascript will gather data and then run the <code>$.ajax</code>.</p></li> <li><p>Will the server side <code>ModelState.IsValid</code> work?</p></li> <li><p>How can I forward validation problem back to the client and present it as if Im using the build int validation(<code>@Html.ValidationSummary(true)</code>)?</p></li> </ol> <p>Example of Ajax call : </p> <pre><code>function SendPost(actionPath) { $.ajax({ url: actionPath, type: 'POST', dataType: 'json', data: { Text: $('#EditPostViewModel_Text').val(), Title: $('#EditPostViewModel_Title').val() }, success: function (data) { alert('success'); }, error: function () { alert('error'); } }); } </code></pre> <p>Edit 1: </p> <p>Included on page : </p> <pre><code>&lt;script src="/Scripts/jquery-1.7.1.min.js"&gt;&lt;/script&gt; &lt;script src="/Scripts/jquery.validate.min.js"&gt;&lt;/script&gt; &lt;script src="/Scripts/jquery.validate.unobtrusive.min.js"&gt;&lt;/script&gt; </code></pre>
    singulars
    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.
 

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