Asp.net mvc Html.BeginForm可以工作,但Ajax.BeginForm不能
我有一个ASP MVC应用程序正在尝试提交我的ViewModel,它有一个名为Asp.net mvc Html.BeginForm可以工作,但Ajax.BeginForm不能,asp.net-mvc,ajax.beginform,Asp.net Mvc,Ajax.beginform,我有一个ASP MVC应用程序正在尝试提交我的ViewModel,它有一个名为Document的属性,它是一个HttpPostedFileBase。当我使用@Html.BeginForm时,ViewModel绑定良好,但是如果我将其更改为@Ajax.BeginForm,并保持所有内容不变,它将绑定除HttpPostedFileBase属性之外的所有ViewModel属性。有什么建议吗 相关代码: [HttpPost] public ActionResult Add(ViewModel
Document
的属性,它是一个HttpPostedFileBase
。当我使用@Html.BeginForm
时,ViewModel绑定良好,但是如果我将其更改为@Ajax.BeginForm
,并保持所有内容不变,它将绑定除HttpPostedFileBase
属性之外的所有ViewModel属性。有什么建议吗
相关代码:
[HttpPost]
public ActionResult Add(ViewModel vm)
{
return new HttpStatusCodeResult(200);
}
@using (Ajax.BeginForm("Add", "Home", new AjaxOptions() { HttpMethod = "Post" , AllowCache = false}, new { enctype = "multipart/form-data" }))
{
@Html.HiddenFor(m => Model.Document.DocumentType);
@Html.HiddenFor(m => Model.Document.DocumentTypeId);
@Html.HiddenFor(m => Model.Document.File);
<div class="container">
<table>
<tr>
<td>
<input class="form-control" type="text" id="lblAllReceivables" /> </td>
<td >
@Html.TextBoxFor(m => m.Document.File, new { type = "file", @class = "inputfile", @name = "file", @id = Model.Document.DocumentTypeId, @accept = ".pdf, .doc, docx" })
<label id="lblUpload" for="@Model.Document.DocumentTypeId"><i class="fa fa-upload" style="margin-right:10px;"></i>File</label>
</td>
</tr>
<tr>
<td colspan="2" >
Comments:<br />
<div class="input-group" style="width:100%;">
@Html.TextAreaFor(m => m.Document.Comments)
</div>
</td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<tr><td colspan="2" > <input id="btnSubmit" class="btn btn-default btn-lg" type="submit" style="width:275px;" value="Submit Application" /><a class="btn btn-default">Cancel</a></td></tr>
</table>
</div>
}
[HttpPost]
公共操作结果添加(ViewModel虚拟机)
{
返回新的HttpStatusCodeResult(200);
}
@使用(Ajax.BeginForm(“添加”、“主页”、新的AjaxOptions(){HttpMethod=“Post”,AllowCache=false},新的{enctype=“multipart/form data”}))
{
@HiddenFor(m=>Model.Document.DocumentType);
@HiddenFor(m=>Model.Document.DocumentTypeId);
@HiddenFor(m=>Model.Document.File);
@Html.TextBoxFor(m=>m.Document.File,新的{type=“File”、@class=“inputfile”、@name=“File”、@id=Model.Document.DocumentTypeId、@accept=“.pdf、.doc、docx”})
文件
评论:
@Html.TextAreaFor(m=>m.Document.Comments)
取消
}
我发现浏览器不支持通过xmlhttprequest上传文件,而这正是ajax.beginform用来发布数据的方式(所有浏览器ajax库也是如此)。如果您使用的是HTML5浏览器,则可以使用新的文件api上载文件。对于较旧的浏览器,可以使用iframe发布文件。google for jquery插件,它包装了这两个函数,或者只使用iframe appraoch(相当繁琐)
在特定情况下,我更喜欢使用其他插件,比如更好地处理它,您可以轻松上载多个文件。您可以显示服务器端代码吗?您不能使用
Ajax.BeginForm()
上载文件。使用$.ajax()
和FormData
并设置正确的ajax选项-参考示例这可能是问题所在,我想我必须使用@Html.BeginForm。非常感谢。