简体   繁体   English

使用jQuery Form插件从MVC 5中的jquery弹出窗口上传文件

[英]File Upload from jquery popup in MVC 5 with jQuery Form plugin

In my MVC 5 web application I need to upload file from jquery popup; 在我的MVC 5 Web应用程序中,我需要从jquery弹出窗口上传文件; in which containing MVC view model. 其中包含MVC视图模型。 I am referring answered question from here . 我是指从这里回答的问题。 According to this post I have defined my view as follows 根据这篇文章,我的观点如下

@using VirtuOx.Views.Shared.Resources
@model VirtuOx.Models.Common.PatientDocuments
@{
    Layout = "~/Views/Shared/_LayoutBlank.cshtml";
}
<script src="@Url.Content("~/Scripts/jquery.form.js")" type="text/javascript"></script>
@using (Html.BeginForm("UploadFile", "Common", FormMethod.Post, new { id = "frmReadingFiles", enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary();
    @Html.HiddenFor(m => m.HideDelete)
    @Html.HiddenFor(m => m.HideUpload)
    <table>
        <tr>
            <td class="align_right" style="width:20%;">@Html.LabelFor(m => m.hdReadingID)</td>
            <td>@Html.DisplayFor(m => m.hdReadingID)@Html.HiddenFor(m => m.hdReadingID)</td>
        </tr>
        <tr>
            <td class="align_right">@Html.LabelFor(m => m.PatientName)</td>
            <td>@Html.DisplayFor(m => m.PatientName)@Html.HiddenFor(m => m.PatientName)</td>
        </tr>
        @if (Model.HideUpload == "False")
        {
            <tr>
                <td class="align_right">@Html.LabelFor(m => m.FiltType)</td>
                <td>@Html.DropDownListFor(m => m.FiltType, new SelectList(Model.FileTypeList, "Value", "Text"), new { @class = "chzn-select-no-single" })</td>
            </tr>
            <tr>
                <td class="align_right">@Html.LabelFor(m => m.File)</td>
                <td>@Html.TextBoxFor(m => m.File, new { type = "file", name = "File[0]" })</td>
            </tr>
        }
    </table>
    if (Model.HideUpload == "False")
    {
        <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
            <div class="ui-dialog-buttonset">
                <input type="submit" class="btn blue" id="btnUpload" value='@VirtuOxCommon.PatientDocuments_Button_Upload' />
                <input type="button" class="btn blue" id="PatientDocuments_btnClose" value="@VirtuOxCommon.PatientDocuments_Button_Close" />
            </div>
        </div>
    }
}
@{Html.RenderPartial("_PatientDocuments", new ViewDataDictionary { { "ReadingID", Model.hdReadingID }, { "HideDelete", Model.HideDelete } });  }
@if (Model.HideUpload == "True")
{
    <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
        <div class="ui-dialog-buttonset">
            <input type="button" class="btn blue" id="PatientDocuments_btnClose" value="@VirtuOxCommon.PatientDocuments_Button_Close" />
        </div>
    </div>
}
<script type="text/javascript">
    $(document).ready(function () {
        $('#frmReadingFiles').ajaxForm({});

        $("#PatientDocuments_btnClose").click(function (e) {
            $(this).parents("div").find(".ui-dialog-content").dialog('close');
        });
    });
</script>

I am opening this view in jquery modal dialog. 我正在jQuery模式对话框中打开此视图。

& my post action method is as follows &我的后期处理方法如下

 public ActionResult UploadFile(Common.PatientDocuments Documents, HttpPostedFileBase File)
    {
        try
        {
            byte[] data;
            using (Stream inputStream = File.InputStream)
            {
                MemoryStream memoryStream = inputStream as MemoryStream;
                if (memoryStream == null)
                {
                    memoryStream = new MemoryStream();
                    inputStream.CopyTo(memoryStream);
                }
                data = memoryStream.ToArray();
            }
            if (data.Length > 0)
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    string path = string.Empty;
                    byte[] objContext = null;
                    DataSet ds = DB.ExecuteDataset("TrustedConnectionString", "pc_ADMReadingFileAdd",
                                            new SqlParameter("@SystemNumber", 1),
                                            new SqlParameter("@FileType", System.IO.Path.GetExtension(File.FileName)),
                                            new SqlParameter("@ReadingID", Documents.hdReadingID),
                                            new SqlParameter("@FileTypeID", Documents.FiltType),
                                            new SqlParameter("@FileName", System.IO.Path.GetFileNameWithoutExtension(File.FileName)),
                                            new SqlParameter("@CustomerID", SessionManager.GetSession().CustomerID));


                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        path = Convert.ToString(ds.Tables[0].Rows[0]["FilePath"]);
                        objContext = (byte[])ds.Tables[0].Rows[0]["TransactionContext"];
                        SqlFileStream objSqlFileStream = new SqlFileStream(path, objContext, FileAccess.Write);
                        objSqlFileStream.Write(data, 0, data.Length);
                        objSqlFileStream.Close();
                        ts.Complete();
                    }
                }
            }
        }
        catch (Exception expObj)
        {
            ModelState.AddModelError("Error", "Error occured while uploading file " + expObj.StackTrace);
        }
        Documents.FileTypeList = Common.GetFileTypes();
        return PartialView("~/Views/Shared/PatientDocuments.cshtml", Documents);
    }

When I select the file & clicks Upload button the required post action method gets executed but view is not reloaded; 当我选择文件并单击“上载”按钮时,将执行所需的后期处理方法,但不会重新加载视图; which results in jqGrid on popup not being reloaded to show newly uploaded file record. 这导致弹出窗口上的jqGrid无法重新加载以显示新上传的文件记录。

Can anybody tell me whats happening wrong in the flow & why the popup view is not reloaded after successful execution of ajaxForm post method. 谁能告诉我流程中发生了什么问题以及成功执行ajaxForm post方法后为何不重新加载弹出视图的原因。

Please Note that after successful execution of post method popup will remain as it is in browser & when I close it & reopens, it will show the jqGrid with uploaded file record 请注意,成功执行post方法后,弹出窗口将保留在浏览器中,当我关闭并重新打开时,它将显示带有已上传文件记录的jqGrid

Can anybody tell me whats happening wrong in the flow & why the popup view is not reloaded after successful execution of ajaxForm post method. 谁能告诉我流程中发生了什么问题以及成功执行ajaxForm post方法后为何不重新加载弹出视图的原因。

Because there's no code in what you have shown that would reload it. 因为您所显示的内容中没有代码可以重新加载它。

If you want this to happen you could subscribe to the success callback of the AJAX call and reload whatever portion of the view you want: 如果您希望发生这种情况,则可以订阅AJAX调用的成功回调,然后重新加载所需的视图的任何部分:

$('#frmReadingFiles').ajaxForm(function(result) {
    // Inject the result in the desired portion of your DOM:
    $('#some_container_id').html(result);
});

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

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