我使用的是asp.net MVC4。我在这里搜索了许多其他类似的问题,但它们都没有帮助。
我想在上传文件后刷新我的局部视图- FileList。相反,我现在看到重定向到具有更新的文件列表的页面,但我想在局部视图中刷新此列表。:(.有人能帮上忙吗?实现类似功能的另一种解决方案也是可以接受的:)
我的部分视图在另一个(索引)部分视图中。
下面是FileController:
private static int folderId = 0;
public ActionResult Index(int id)
{
folderId = id;
this.GetFiles();
return PartialView();
}
public PartialViewResult FileList()
{
this.GetFiles();
return PartialView("FileList");
}
[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult UploadFiles(IEnumerable<HttpPostedFileBase> filesToUpload)
{
this.UploadFilesToBase(filesToUpload, 1);
this.GetFiles();
return PartialView("FileList");
}这是Index.cshtml
<script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-2.6.2.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/main.js")" type="text/javascript"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
@using (Ajax.BeginForm("UploadFiles", "File", null, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "FilesListDiv", InsertionMode = InsertionMode.Replace }, new { enctype = "multipart/form-data" }))
{
<div class="btn-upload-file">
@Html.AntiForgeryToken()
<span class="btn"><span>Upload file</span></span>
<input type="file" class="file" name="filesToUpload" multiple onchange="javascript:this.form.submit();"><br>
</div>
}
<div id="FilesListDiv">
@*@Html.Action("FileList", "File")*@
@Html.Partial("~/Views/File/FileList.cshtml")
</div>这是我的FileList.cshtml
@{
Layout = null;
ViewBag.Title = "Files";
}
@foreach (MyProj.Web.DataContracts.File file in ViewBag.Files)
{
file.Name
}发布于 2014-02-04 20:49:49
您的ajax表单无法工作,可能是因为您没有在页面中包含jquery.unobtrusive-ajax.js,因此,它的工作方式与常规html表单类似。
一旦添加了这个文件,您就可以通过ajax刷新内容,但是控制器中的filesToUpload将是null。原因是无法通过ajax上传文件,因为它不支持multipart/form-data加密。
你可以尝试在HTML 5 File API中使用一些jQuery上传插件。
发布于 2014-02-04 21:50:18
使用此插件http://malsup.com/jquery/form/,无需刷新即可提交表单。当你将文件上传到服务器上时,你可以在上传成功时进行回调。
从服务器获取文件列表,作为html呈现(通过partialresult),并在客户端浏览器上更新它们。
https://stackoverflow.com/questions/21551816
复制相似问题