0

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

<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");" 在完成所有这些以添加附件之后,整个屏幕都被刷新了,我只想刷新用于添加附件的部分视图。我怎么能做到这一点!?

4

1 回答 1

2

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

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

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

或者

$("someButton").click(function(){
    $('#myForm1').ajaxSubmit();
});
于 2014-02-27T07:10:28.567 回答