使用 jQuery 从子视图关闭对话框,而无需在 MVC3 中重新加载父视图



我要实现的场景如下:

我正在使用带有剃须刀视图的 MVC 3。

单击文本框(

称为"结果文本框")时,从主视图(这是不同的视图,即"添加视图")打开一个对话框,并携带回发到主控制器操作。

在对话框(即"添加视图")上,只需输入两个数字进行添加,并在提交时执行"添加视图控制器"操作的回发。

现在,当添加控制器操作处理完成时,我想添加数字并关闭当前对话框(添加视图),并使用添加结果更新父视图(主页视图)上显示的结果文本框。

注意:关闭对话框后,我不希望重新加载或刷新父视图。

现在,我面临的问题是如何

1)获取隐藏字段值(容器添加值)并设置为父页面上的结果测试框?

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();
        }

得到了解决方案。

我没有在对话框视图上使用 jQuery 函数,而是在父视图本身上使用对话框按钮。

由于对话框在父 .cshtml 上的 javascript 函数中被定义为全局变量,并将子视图封装在div 中,因此可以访问两个视图上的控件。

不确定这是否是你的追求,但是,在子帧中,您可以设置一个全局变量,然后将父帧中的控制文本设置为可用

parent.document.getElementById("").text() = "";

然后,您可以通过执行以下操作关闭子框架

var child = document.getElementById('childId');
child.document.close()

希望这有帮助

相关内容

最新更新