jquery - Controller can return either JSON or HTML depending on the result -


suppose have pop-up window contains form. must have controller processes form, , depending on result, controller returns either json (if goes well, , popup can closed javascript) or html (if form data not valid , form must replaced new html - validation error messages). found such solution: that's form:

<form id="message" ...>     ... </form> 

and have jquery handler form:

$(document).on("submit", "form#message", function (evt) {     evt.preventdefault();     $.ajax({         type: this.method,         url: this.action,         data: $(this).serialize(),         success: function (result) {             if ($.isplainobject(result)) {                 // json                 // close pop-up window             } else {                 // html                 $("form#message").html(result);             }         }     }); }); 

controller:

[httppost] public actionresult updatemessage(messagemodel model) {     ...     if (.. valided ..)         return json(new { success = "ok" });     else         return view(model); } 

the question - there more elegant solutions such tasks?

imho nice solution problem , use.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -