jQuery Validate, ASP.NET MVC ModelState Errors (Async POST) -


I am developing a web app with asp.net mvc 3 and I have some form that has an async Is the post for (by ajax). This action receives a view modal with some data annotation to verify it. Validity works fine, but when the vendor returns an error, I do not know how I can show it in my scene (because post was done by AJAX).

My action is like this:

  [HTTP post] Public Action Result Service Customer (Customer View Model Input) {if (! ModelState. ISI Walid) {// <- Business Verification Return JS (New {Success = false, Errors = ???}); } // Continuing Jason (new {success = true}); }   

How can I show these errors with jquery valid in my view? If it is possible to post some code to sample some ... I will like it!

Thanks guys! Instead of sending JSON in the case of error, I will put the form inside partial and then the action of the administrator of the error is done.

In case the matter is to be partially returned. The problem with JSON is that you can actually get errors from ModelStat using LINQ, but it can show them to see PATA.

Like this:

  & lt; Div id = "myform" & gt; @HTMLPartial ("_moform") & lt; / Div & gt;   

and then inside _MyForm.cshtml :

  @model CustomerViewModel @using (Html.BeginForm ()) { @Html .EditorFor (x = & gt; x.Foo) @ html.ValidationMessageFor (x = & gt; x.Foo) & lt; Br / & gt; @ Html.EditorFor (x = & gt; x.Bar) @ html.ValidationMessageFor (x => x.Bar) & lt; Br / & gt; & Lt; Input type = "submit" value = "ok" /> }   

and the Administrator's action will be taken:

  [HTTP post] Public action SaveCustomer (Customer View Model Model) {if (ModelState.IsValid) {Return PartialView ("_movorm", model); } Returns Jason (new {success = true}); }   

And the last step is to AJAXify this form which can be done in a separate JavaScript file:

  $ (function () {$ ( 'Representative' ('form', 'submit', function () {$ .ajax ({url: this.action, type: this.method, data: $ (this). Serialize (), success: function (results) {If (result.success) {// In the case of success ('success') we have the JSON object;} else {// We are partial with errors in case of failure / so we have to do all DOM $ ( '# Myform'). Html (result);}}}) return false;});});    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -