从局部视图打开弹出窗口并重新加载父部分视图

时间:2013-08-12 20:46:10

标签: javascript asp.net-mvc asp.net-ajax

我尝试了不同的方式,使用javascript和ajax。如果您在

之前遇到这种情况,请提供帮助

我有一个视图hfnlp.cshtml

@model IMONLP.Models.HFADMwrap
@using ADM
@{
    ViewBag.Title = "HFNLP";
}
<html>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<body>


**<div id="popup">
    @if (true)
    {
      Html.RenderPartial("popup", Model);
    }
</div>**

<div id="Quadrant">
        @if (@Model.flag == 1)
        {
            Html.RenderPartial("grid", Model);
        }
    </div>


</body>
</html>

我有这个popup.cshtml,它是上面视图的局部视图。会有一个弹出窗口打开并从用户获取内容。 它必须做这些事情。 1.获取内容,将其写入模型 2.关闭弹出窗口 3.将控件恢复为父级,父级不应重新加载,如果popup.cshtml(部分视图) - [父级winodw重新加载的一部分]重新加载,我很好。

这是popup.cshtml

@model IMONLP.Models.HFADMwrap

@{
    ViewBag.Title = "popup";
}
<html>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script type="text/javascript">

    //pop up window
    $(function () {
        $('#ClickMe').click(function () {
            window.open('@Url.Action("FileUpload", "HFNLP")', 'FileUpload', 'height=' + (window.screen.height - 100) + ',width=700,left=' + (window.screen.width - 250) + ',top=10,status=no,toolbar=no,resizable=yes,scrollbars=yes');
        });
    });
</script>

<body>

    <input type="button" id="ClickMe" name="ClickMe" value="New Visit"/>

</body>

</html>

单击按钮应打开弹出窗口


最后,fileupload.cshtml是点击按钮时调用的弹出窗口

@model IMONLP.Models.HFADMwrap
@using ADM
@{
    ViewBag.Title = "FileUpload";
}

<html>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>


<script type="text/javascript">
    $(function () {
        $('#ClickMe').click(function () {

           //window.opener.location.href = '@Url.Action("AfterUpload", "HFNLP", new { wrap=@Model})';*@
           window.opener.location.href = window.opener.location.href;
          window.close();
        });
    });
</script>
<body>

    <br />
    @using (Html.BeginForm("AfterUpload", "HFNLP"))
    {

                            <input type="button" value="submit & View Codes" style="position: absolute; left: auto; width: auto;" id="ClickMe" name = "ClickMe"/>
                           @*<input type="hidden" name="a" value="@Model.Hfnlp.unstructured_text"/>*@


                            @if (!string.IsNullOrEmpty(Model.Hfnlp.unstructured_text))
                            {
                            @Html.TextAreaFor(m => m.Hfnlp.unstructured_text, new { name= "textBox1", @value = Model.Hfnlp.unstructured_text, style = "width: 650px; height: 400px;"})
                            }
                            else
                            {
                            @Html.TextAreaFor(m => m.Hfnlp.unstructured_text, new { style = "width: 650px; height: 400px;" })
                            }


    }       
        @using (Html.BeginForm("FileUploadw", "hfNLP", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {

            <input type="file" name="file" id="file" /><br />
            <input type="submit" value="Upload" name="submit" />           
        }

</body>
</html>

使用此代码,我可以获得弹出窗口并调用控制器的动作并关闭弹出窗口。然后,整个父页面重新加载。我正在丢失传递给模型的数据。请帮助我摆脱这种情况。

** * ** * ** * 的** 修改 * ** * ** * ** * *

public ActionResult FileUpload(HFADMwrap wrap)
        {
            HFNLP hf = new HFNLP();
            wrap.Hfnlp = hf;
            return View("FileUpload", wrap);
        }

        public ActionResult FileUploadw(HFADMwrap wrap, HttpPostedFileBase file)
        {
            HFNLP hf = new HFNLP();
            if (wrap.Hfnlp == null)
            {
                wrap.Hfnlp = hf;
            }
            file = Request.Files[0];
            BinaryReader b = new BinaryReader(file.InputStream);
            byte[] binData = b.ReadBytes(file.InputStream.Length.ToInt32());
            string filecontent = System.Text.Encoding.UTF8.GetString(binData);
           hf.unstructured_text = filecontent;
            /*upload and get the file content */

            if (wrap.Hfnlp.unstructured_text == null)
            {
                return RedirectToActionPermanent("FileUpload", "FileUpload");
            }
            wrap.Hfnlp = hf;
            return PartialView("FileUpload", wrap);
        }

我现在试过这个

<div id="popup">
<input type="button" value="something" onClick="?(What should be here)"/></div>

1 个答案:

答案 0 :(得分:0)

我使用jquery ui对话框进行这样的工作。没有理由在视图上加载部分右侧。

在我的* .cshtml中,我有一个div,最终会成为我的对话

<div id="delegateDialog" class="actionDialog"></div>

然后,当单击特定按钮时,将调用此方法来加载对话框:

var loadDelegateDialog = function(processInstanceID, serialNumber) {
    var $dialog = $('#delegateDialog');
    var title = "Delegate Workflow #" + processInstanceID;
    var actionUrl = '@Url.ActionFor((TaskListController c) => c.GetDelegateDialog(Model.TaskListID, null, null))';

            $.ajax({
                url: actionUrl,
                type: 'POST',
                data: {
                    processInstanceID: processInstanceID,
                    serialNumber: serialNumber
                },
                dataType: 'html',
                success: function(data) {
                    $dialog.html(data);

                    $dialog.dialog({
                        autoOpen: true,
                        closeOnEscape: false,
                        modal: true,
                        resizable: false,
                        draggable: false,
                        dialogClass: "no-close",
                        maxHeight: 600,
                        width: 500,
                        position: { my: "top", at: "top+20", of: window },
                        hide: { effect: "fade", duration: 250 },
                        show: { effect: "fade", duration: 250 },
                        title: title,
                        close: function () {
                            // call a method to reload your parent page via ajax
                        }
                    });
                },
                error: function(jqXhr, textStatus, errorThrown) {
                    $.notifyBar({
                        html: jqXhr.responseText,
                        cls: 'error',
                        delay: 10000
                    });
                }
            });
        };

loadDelegateDialog js方法调用我的控制器GetDelegateDialog,返回局部视图。然后将内容放入对话框中。

这是控制器方法:

[HttpPost]
public PartialViewResult GetDelegateDialog(int id, int? processInstanceID, string serialNumber)
{
    var taskListID = id;

    if (!processInstanceID.HasValue)
    {
        throw new InvalidOperationException(Resources.Web_ErrorMsg_MissingProcessIntanceID);
    }

    if (string.IsNullOrWhiteSpace(serialNumber))
    {
        throw new InvalidOperationException(Resources.Web_ErrorMsg_MissingSerialNumber);
    }

    var model = this.taskListService.BuildDelegateDialogViewModel(taskListID, processInstanceID.Value, serialNumber);

    return this.PartialView("_DelegateDialog", model);
}

让对话框的close方法调用另一个可以刷新/重新加载页面的js方法,或者,如果你不想完全重新加载,可以调用AJAX来获取你想要的数据并填充您页面的相应部分。