【问题标题】:Update only partial view when submit button for file upload is clicked单击文件上传的提交按钮时仅更新部分视图
【发布时间】:2014-03-30 10:56:42
【问题描述】:

我有一个允许用户提交文件的局部视图。

<form action="Attachments" method="post" enctype="multipart/form-data" target="_self">
    <div>
        <label style="text-align: left;">Add File:</label>
    </div>
    <div>
        <input type="file" name="file" id="file" style="width: 275px;" />
    </div>
    <div style="text-align: right;">
        <input type="submit" value="Add"/>
    </div>
</form>

这会调用 HTTPPost 的 AttachmentsController 方法

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{

    if (file.ContentLength > 0)
    {
        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine("C:\\Attachments", fileName);
        file.SaveAs(path);

        return AddAttachment(fileName);
    }

    return RedirectToAction("Failed", "Attachments");
}

addAttachment 返回 "return RedirectToAction("Index", "Attachments");" 在完成所有这些以添加附件之后,整个屏幕都被刷新了,我只想刷新用于添加附件的部分视图。我怎样才能做到这一点!?

【问题讨论】:

    标签: asp.net-mvc partial-views ajax-forms


    【解决方案1】:

    您应该通过 ajax 请求发布您的文件。当服务器返回响应时,只更新必要的页面部分。

    有一个不错的表单插件,可让您异步发送 HTML 表单http://malsup.com/jquery/form/

    $(document).ready(function() { 
        $('#myForm1').ajaxForm(); 
    });
    

    $("someButton").click(function(){
        $('#myForm1').ajaxSubmit();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-22
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多