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
Post a Comment