Note that there are some explanatory texts on larger screens.

plurals
  1. POClose modal window after submit form
    primarykey
    data
    text
    <p>I've searched all over looking at similar questions but unfortunately none seem to quite work for me.</p> <p>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:</p> <pre><code>&lt;div id="uploadResults"&gt; @using (Html.BeginForm("_uploadDocument", "Request", FormMethod.Post, new { id = "uploadDoc", enctype = "multipart/form-data" })) { &lt;input name="eGuid" value="@ViewBag.eGuid" id="eGuid" type="hidden" /&gt; &lt;input name="result" id="result" value="@ViewBag.result" type="hidden" /&gt; &lt;label for="attachment"&gt;Attachment:&lt;/label&gt;&lt;input type="file" name="attachment" id="attachment" /&gt; &lt;br /&gt; &lt;input type="submit" name="submit" value="Upload" class="txtButton" /&gt; } &lt;/div&gt; </code></pre> <p>On submit of the form I'd like to call my _uploadDocument method in the controller. 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.</p> <p>My controller for the httppost (apologies, I've tried several methods and so you'll see a few commented out bits):</p> <pre><code>[HttpPost] public void _uploadDocument(string eGuid, HttpPostedFileBase attachment) { // store result txt string result = ""; // check for uploaded file if (attachment != null &amp;&amp; attachment.ContentLength &gt; 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); } </code></pre> <p>Within my modal window, where the form above is, I have tried the following jquery:</p> <pre><code>$("#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; }); </code></pre> <p>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).</p> <p>If I use the code as above then the document is uploaded but I'm taken to a blank white screen after submit...</p> <p>I'd certainly appreciate any pointers!</p> <p>UPDATE Here's the code opening the modal window from the parent page:</p> <pre><code>// 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'); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload