KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Suppose I have a pop-up window that contains a form. I must have a controller that processes the form, and depending on the result, this controller returns either JSON (if all goes well, and popup can be closed by javascript) or HTML (if the form data is not valid and form must be replaced with a new html - with the validation error messages). So I found just such a solution: that's form: <form id="message" ...> ... </form> And I have jquery handler for this 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)) { // this is JSON // close the pop-up window } else { // this is HTML $("form#message").html(result); } } }); }); Controller: [HttpPost] public ActionResult UpdateMessage(MessageModel model) { ... if (.. is valided ..) return Json(new { success = "OK" }); else return View(model); } The question - are there more elegant solutions for such tasks?
Tags (comma-separated)
Save Edits
Cancel