単純なものが欠けていると思いますが、同じコードを何度も見て目がくらみ、別の目を使うことができます。
Form を含む MVC 部分ビューがあります。
<div class="Clear"></div>
<div class="OverFlow">
@using (Html.BeginForm(null, null, FormMethod.Post, new {id = "formId"}))
{
<div class="ErrorBx1">@Html.GetData()</div>
<table>
@Html.EditorFor(m => m.FormModel, "MyEditorTemplate")
<tr>
<td colspan="2"></td>
<td><input type="image" src="@Images.ResolveDefault("btn-send.jpg")" alt="Send" class="Send" /></td>
</tr>
</table>
}
</div>
このビューには Javascript も含まれています
<script type="text/javascript">
$(function () {
$('#formId').submit(function () {
$.ajax({
cache: false,
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (res) {
if (res.success) {
alert("success");
closeDialog();
window.parent.specialFunction();
} else {
alert("not success");
$('#someElement').replaceWith(res);
}
}
});
return false;
});
});
</script>
実行されるコントローラーメソッドは次のとおりです
[HttpPost]
public ActionResult Index(MyViewModel viewModel)
{
if (CheckSuccess())
{
return Json(new { success = true });
}
return ViewNoLayout("otherView", viewModel);
}
このビューは、jQuery.UI ダイアログに読み込まれます。ダイアログが初めて読み込まれるときに、フォームの送信ボタンをクリックすると、success 関数が正しく実行されます。警告が表示され、ダイアログが閉じられ、親関数が呼び出されます。ダイアログを再度ポップアップして送信ボタンをクリックすると、呼び出しはサーバーに送られ、正しく処理されますが、ページがリロードされ、JSON 文字列のみが表示されます (アラートなどは表示されません)。私が見逃しているある種のクライアント側キャッシング、または同等のものがあると想定しています。何か案は?