假设我有一个包含表单的弹出窗口.我必须有一个处理表单的控制器,根据结果,该控制器返回
JSON(如果一切顺利,弹出可以通过javascript关闭)或HTML(如果表单数据无效且表单必须被替换)使用新的html – 带有验证错误消息).
所以我找到了这样一个解决方案:形式如下:
...
我有这个表单的jquery处理程序:
$(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);
}
}
});
});
控制器:
[HttpPost]
public ActionResult UpdateMessage(MessageModel model)
{
...
if (.. is valided ..)
return Json(new { success = "OK" });
else
return View(model);
}
问题 – 这些任务有更优雅的解决方案吗?