フォームを含むポップアップ ウィンドウがあるとします。フォームを処理するコントローラーが必要です。結果に応じて、このコントローラーは JSON (すべてがうまくいき、JavaScript でポップアップを閉じることができる場合) または HTML (フォーム データが無効でフォームを置き換える必要がある場合) を返します。新しい html を使用 - 検証エラー メッセージを使用)。だから私はまさにそのような解決策を見つけました:それはフォームです:
<form id="message" ...>
...
</form>
そして、私はこのフォームの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);
}
質問 - そのようなタスクのためのより洗練されたソリューションはありますか?