简体   繁体   English

Ajax.BeginForm PartialView不刷新

[英]Ajax.BeginForm PartialView not refreshing

I am using asp.net mvc 4. I've searched many other similar questions here, but they didn't help. 我正在使用asp.net mvc4。我在这里搜索了许多其他类似的问题,但是它们没有帮助。

I want to refresh my partial view - FileList after uploading file. 我想在上传文件后刷新我的局部视图-FileList。 Instead I see now redirecting to page with updated file list, but i want to refresh this list in partial view. 相反,我现在看到重定向到具有更新文件列表的页面,但是我想在部分视图中刷新此列表。 :(. Can anybody help? Another solutions to do similar features are acceptable :) :(。有人可以帮忙吗?可以使用另一种实现类似功能的解决方案:)

My partial view is in another (index) partial view. 我的局部视图在另一个(索引)局部视图中。

Here is FileController: 这是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");
}

and here is Index.cshtml 这是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>

and here is my FileList.cshtml 这是我的FileList.cshtml

 @{
    Layout = null;
    ViewBag.Title = "Files";
 }

 @foreach (MyProj.Web.DataContracts.File file in ViewBag.Files)
 {         
    file.Name
 }

Your ajax form doesn't work, probably because you haven't included jquery.unobtrusive-ajax.js into the page and as a result, it works like a regular html form. 您的ajax表单不起作用,可能是因为您没有在页面中包含jquery.unobtrusive-ajax.js ,因此,它的工作方式类似于常规的html表单。

As soon as you add this file, you will be able to refresh the content via ajax, but filesToUpload will be null in the controller. 添加此文件后,您将能够通过ajax刷新内容,但是filesToUpload将为null The reason for that is impossibility of uploading files via ajax, because it does not support multipart/form-data enctype. 这样做的原因是无法通过ajax上传文件,因为它不支持multipart/form-data编码类型。

You can try to use some jQuery upload plugins ot HTML 5 File API . 您可以尝试使用HTML 5 File API的一些jQuery上传插件。

Use this plugin http://malsup.com/jquery/form/ and this will submit form without the refresh. 使用此插件http://malsup.com/jquery/form/ ,它将无需刷新即可提交表单。 When you got the file uploaded on server then you can make a callback on the success of upload. 将文件上传到服务器后,可以对上传成功进行回调。

Get the list of file from server as render as html (through partialresult) and update them on client side browser. 从服务器获取的文件列表以html格式呈现(通过partialresult),并在客户端浏览器上进行更新。

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

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