実装したいシナリオは次のように述べられています。
Razor ビューで MVC 3 を使用しています。
テキスト ボックス (結果テキスト ボックスと呼びます) をクリックすると、ホーム ビュー (別のビュー、ビューの追加) からダイアログが開き、ホーム コントローラー アクションへのポストバックが実行されます。
ダイアログ ([ビューの追加]) で、追加する 2 つの数値を入力するだけで、その送信時に、[ビュー コントローラーの追加] アクションでポストバックが実行されます。
ここで、コントローラーの追加アクションの処理が完了したら、数値の追加を行い、現在のダイアログ (ビューの追加) を閉じ、親ビュー (ホーム ビュー) にある結果テキスト ボックスを追加結果で更新します。
注: ダイアログを閉じた後、親ビューを再読み込みまたは更新したくありません。
今、私が問題に直面しているのは、
1) 隠しフィールドの値 (コンテナの追加値) を取得し、親ページの Result テスト ボックスに設定しますか?
2) ダイアログ自体からダイアログ (ビューの追加) を閉じますか? 親ビューを更新せずに?
コード:
<h2> Input numbers to add</h2>
@using (Html.BeginForm("About","Home",FormMethod.Post, new {id = "dialogchildform"}))
{
@Html.Hidden("hdnresult", ViewData["result"], new { id = "hdnres" })
<div style="position: relative; margin-left: 200px; top: 80px">
<fieldset>
@Html.ValidationSummary(true)
<div class="editor-label">
Value 1
</div>
<div class="editor-field">
@Html.EditorFor(model => model.num1, new { id = "num1" })
@Html.ValidationMessageFor(model => model.num1)
</div>
<div class="editor-label">
Value 2
</div>
<div class="editor-field">
@Html.EditorFor(model => model.num2, new { id = "num2" })
@Html.ValidationMessageFor(model => model.num2)
</div>
</fieldset>
<p>
<input type="submit" value="Add and Retrun" id="inputSubmit" />
</p>
</div>
}
<script type="text/javascript">
$(document).ready(function () {
$("#inputSubmit").click(function () {
document.forms('dialogchildform').submit();
// txtParentResult is a Textbox on prent view to be updated
document.getElementById('txtParentResult').value
= document.forms('dialogchildform').hdnres.value;
// not updating the parent textbox
// $(window.document).dialog('close');
//gives error - Microsoft JScript runtime error: Object doesn't support property or method 'dialog'
//$("#mydiag").dialog("close");
// also gives error
//jQuery(".ui-dialog-content").dialog("close");
// again gives error - Microsoft JScript runtime error: Object doesn't support property or method 'dialog'
});
});
</script>
コントローラーのアクション:
[HttpPost]
public ActionResult Add(AddNum vAddNum)
{
objAddNum = vAddNum;
int result = objAddNum.AddNumbers();
ViewData["_ActionCloseDialog"] = "true";
ViewData["result"] = result;
return View();
}