简体   繁体   English

提交表格后关闭模式窗口

[英]Close modal window after submit form

I've searched all over looking at similar questions but unfortunately none seem to quite work for me. 我搜索了所有类似问题,但不幸的是似乎没有一个对我有用。

What I'm doing: I have a main page with a link to upload a document. 我在做什么:我有一个主页,上面有一个上传文档的链接。 When that link is clicked it opens a modal window with a simple upload form: 单击该链接后,它将打开一个带有简单上传表单的模式窗口:

<div id="uploadResults">
@using (Html.BeginForm("_uploadDocument", "Request", FormMethod.Post, new { id = "uploadDoc", enctype = "multipart/form-data" }))
 {
    <input name="eGuid" value="@ViewBag.eGuid" id="eGuid" type="hidden" />
    <input name="result" id="result" value="@ViewBag.result" type="hidden" />
    <label for="attachment">Attachment:</label><input type="file" name="attachment" id="attachment" />
    <br />
    <input type="submit" name="submit" value="Upload" class="txtButton" />
 }
</div>

On submit of the form I'd like to call my _uploadDocument method in the controller. 提交表单后,我想在控制器中调用_uploadDocument方法。 If the upload succeeds then close the modal window so the parent window is again present. 如果上传成功,则关闭模式窗口,以便再次显示父窗口。 If there is an error then keep the modal window present and display some notification text. 如果有错误,则保持模态窗口存在并显示一些通知文本。

My controller for the httppost (apologies, I've tried several methods and so you'll see a few commented out bits): 我的httppost控制器(抱歉,我已经尝试了几种方法,因此您会看到一些注释掉的位):

[HttpPost]
        public void _uploadDocument(string eGuid, HttpPostedFileBase attachment)
        {
            // store result txt
            string result = "";

            // check for uploaded file
            if (attachment != null && attachment.ContentLength > 0)
            {
                // get filename
                var uploadedFileName = Path.GetFileName(attachment.FileName);
                var newFileName = eGuid + "_" + uploadedFileName;

                // store to the shared directory
                var path = System.Web.Configuration.WebConfigurationManager.AppSettings["UploadDirectory"];
                var savePath = Path.Combine(path, newFileName);
                try
                {
                    attachment.SaveAs(savePath);
                    result = "success";
                }
                catch
                {
                    result = "There was an issue uploading your file";
                }
            }
            else
            {
                result = "No file was chosen for upload";
            }

            ViewBag.result = result;
            ViewBag.eGuid = eGuid;
            //return PartialView("_uploadDocument");
            //return Json(new { result = result});
            //return Json(result);
        }

Within my modal window, where the form above is, I have tried the following jquery: 在上面的表单所在的模式窗口中,我尝试了以下jquery:

$("#uploadDoc").submit(function (e) {
        $.post(this.action, { eGuid: $('#eGuid').val(), attachment: $('#attachment') }, function () {
            $("#dialog5").dialog('close');
            //alert("In");
            //return false;
        });
        //$.post(this.action, { eGuid: $('#eGuid').val(), attachment: $('#attachment') });
        return false;
    });

My results - if I serialize the data passed then the success function fires but the document isn't uploaded (so the modal window closes but no document is uploaded). 我的结果-如果我序列化传递的数据,那么将触发成功函数,但未上载文档(因此,模式窗口关闭,但没有上载文档)。

If I use the code as above then the document is uploaded but I'm taken to a blank white screen after submit... 如果我使用上述代码,则会上传文档,但提交后将进入空白白屏...

I'd certainly appreciate any pointers! 我一定会感激任何指针!

UPDATE Here's the code opening the modal window from the parent page: 更新以下是从父页面打开模式窗口的代码:

// person look-up modal window
    $(function () {
        $('#dialog5').dialog({
            autoOpen: false,
            width: 400,
            resizable: false,
            title: 'Upload Document',
            dataType: "json",
            modal: true,
            open: function (event, ui) {
                var updateArea = $(this).data('area');
                //alert(updateArea);
                $(this).load('@Url.Action("_uploadDocument", "Request")' + '?eGuid=' + updateArea);
            },
            cancel: function (event, ui) {
                $(this).dialog("close");
            }
        });

        $('.attachDocument').live("click", function () {
            var field = this.name;
            $('#dialog5').data('area', field).dialog('open');
        });
    });

You cannot upload files via ajax without using html 5, iframe hacks, etc. It appears to me that your best bet would be to use an uploader such as uploadify or plupload . 如果不使用html 5,iframe hack等文件,则无法通过ajax上传文件。在我看来,最好的选择是使用上载器,例如uploadifyplupload

Binding-httppostedfilebase-using-ajax-beginform is a similar question that has some good suggestions how to get around this. Binding-httppostedfilebase-using-ajax-beginform是一个类似的问题,它提供了一些很好的建议来解决此问题。

It looks like you may have been on the right track, but abandoned it. 看来您可能走了正确的路,但放弃了。

You can't issue a return statement properly from a void. 您不能正确地从void发出return语句。 Have you considered making your uploadDocument method a bool instead of void and returning the success state? 您是否考虑过将popupDocument方法设置为bool而不是void并返回成功状态?

This way you could do something like: 这样,您可以执行以下操作:

$.post(this.action, { eGuid: $('#eGuid').val(), attachment: $('#attachment') }, function (data) {
  if(data == true) {
    $('#uploadResults').hide();
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM