C# Ajax.begin PartialView不刷新
我正在使用asp.NETMVC4。我在这里搜索了许多其他类似的问题,但都没有帮助 上传文件后,我想刷新我的部分视图-文件列表。 相反,我现在看到重定向到带有更新文件列表的页面,但我想在部分视图中刷新此列表:(.有人能帮忙吗?可以接受其他具有类似功能的解决方案:) 我的部分视图位于另一个(索引)部分视图中 以下是FileController:C# Ajax.begin PartialView不刷新,c#,ajax,asp.net-mvc,asp.net-mvc-4,C#,Ajax,Asp.net Mvc,Asp.net Mvc 4,我正在使用asp.NETMVC4。我在这里搜索了许多其他类似的问题,但都没有帮助 上传文件后,我想刷新我的部分视图-文件列表。 相反,我现在看到重定向到带有更新文件列表的页面,但我想在部分视图中刷新此列表:(.有人能帮忙吗?可以接受其他具有类似功能的解决方案:) 我的部分视图位于另一个(索引)部分视图中 以下是FileController: private static int folderId = 0; public ActionResult Index(int id) { fold
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");
}
您的ajax表单不起作用,可能是因为您没有在页面中包含
jquery.unobtrusive ajax.js
,因此,它的工作方式与常规html表单类似
添加此文件后,您将能够通过ajax刷新内容,但控制器中的filesToUpload
将为null
。原因是无法通过ajax上传文件,因为它不支持multipart/formdata
enctype
您可以尝试使用一些jQuery上传插件。使用此插件,此插件将提交表单而无需刷新。当您将文件上传到服务器上时,您可以对上传成功进行回调
从服务器获取以html格式呈现的文件列表(通过partialresult),并在客户端浏览器上进行更新。有关如何刷新部分视图的信息,请参见此处的答案
<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>
@{
Layout = null;
ViewBag.Title = "Files";
}
@foreach (MyProj.Web.DataContracts.File file in ViewBag.Files)
{
file.Name
}