Don*_*ato 8 ajax asp.net-mvc jquery asp.net-mvc-4
假设我有一个包含表单的弹出窗口.我必须有一个处理表单的控制器,并且根据结果,该控制器返回JSON(如果一切顺利,弹出可以通过javascript关闭)或HTML(如果表单数据无效且表单必须被替换)使用新的html - 带有验证错误消息).所以我找到了这样一个解决方案:形式如下:
<form id="message" ...>
...
</form>
Run Code Online (Sandbox Code Playgroud)
我有这个表单的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);
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
控制器:
[HttpPost]
public ActionResult UpdateMessage(MessageModel model)
{
...
if (.. is valided ..)
return Json(new { success = "OK" });
else
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
问题 - 这些任务有更优雅的解决方案吗?
| 归档时间: |
|
| 查看次数: |
905 次 |
| 最近记录: |