0

我尝试了不同的方法,使用了 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(partial view)- [part of parent winodw reloads] reloads 我很好.

这是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>
4

1 回答 1

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 调用以获取所需的数据并填充适当的部分你的页面。

于 2013-08-13T11:34:20.777 回答