[英]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.