简体   繁体   English

单击提交文件上载按钮时仅更新部分视图

[英]Update only partial view when submit button for file upload is clicked

I have a partial view that allows users to submit files. 我有一个部分视图,允许用户提交文件。

<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>

This calls the AttachmentsController method for HTTPPost 这为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");
}

The addAttachment returns "return RedirectToAction("Index", "Attachments");" addAttachment返回“return RedirectToAction(”Index“,”Attachments“);” After all this is done to add the attachment the whole screen is refreshed and I want only the partial view for adding attachments refreshed. 完成所有这些以添加附件后,整个屏幕都会刷新,我只想要部分视图来添加刷新的附件。 How can I accomplish this!? 我怎么能做到这一点!?

You should post your file by ajax request. 您应该通过ajax请求发布您的文件。 And when server returns response, update only necessary page parts. 当服务器返回响应时,仅更新必要的页面部分。

There is a nice form plugin that allows you to send an HTML form asynchroniously http://malsup.com/jquery/form/ . 有一个很好的表单插件,允许您异步发送HTML表单http://malsup.com/jquery/form/

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

or 要么

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

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

相关问题 单击提交按钮时刷新部分视图内容 - Refresh partial view content when submit button clicked 仅在单击提交按钮时才显示数据? - Display the data only when the submit button is clicked? 在提交时仅刷新部分视图 - Refresh only the partial view on submit 当单击部分视图中的按钮时,将局部视图的详细信息转发到控制器的HttpPost方法 - Forwarding the partial view's details to HttpPost method of a controller when button in partial view is clicked 如何刷新/更新 Model 内容而不在没有部分视图的情况下重新加载提交按钮上的视图? - How to refresh/update Model content without reloading the view on submit button without partial view? 使用部分视图MVC 5上传文件 - File Upload with Partial View MVC 5 使用提交时另一个局部视图中的字符串更新局部视图? - Update a partial view with a string from another partial view on submit? 在DIV中打开部分视图,然后使用“提交”按钮 - Open partial view in a DIV and use Submit button MVC视图:如何仅在单击按钮时在视图中呈现表 - MVC views: How to render a table in a view only when a button clicked 在ASP.net MVC的部分视图中的模式弹出提交按钮上调用jQuery函数时未调用 - Jquery function not called when called on modal pop up submit button in partial view in ASP.net MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM