0
在我的MVC 5 web應用程序中,我需要從jquery彈出窗口上傳文件;其中包含MVC視圖模型。我指的是here的回答問題。根據這篇文章,我已經定義了我的觀點如下從jQuery中彈出的文件從jQuery窗體插件上傳jQuery表單插件
@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>
我打開jQuery模式對話框中的這個視圖。
&我的崗位操作方法如下:
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);
}
當我選擇&點擊上傳按鈕所需的後操作方法被執行,但鑑於沒有裝載文件;這導致彈出窗口上的jqGrid沒有被重載以顯示新上傳的文件記錄。
有沒有人可以告訴我流程中發生了什麼錯誤&爲什麼在成功執行ajaxForm post方法後沒有重新加載彈出視圖。
請注意,POST方法彈出的成功執行後,仍將因爲它是在瀏覽器&當我關閉它&重新打開,它會顯示的jqGrid與上傳的文件記錄