HTTPPostedFileBase总是在控制器中返回null [英] HTTPPostedFileBase always return null in controller
问题描述
我已经为此检查了各种答案,但是它们都不适合我,.cshtml中的代码如下:
I have checked various answer for this, but none of them works for me, my code in .cshtml is as follows:
<input type="file" name="PostedNRLFile" />
然后在控制器中我拥有
public JsonResult SaveRecord(NewAuditLetterViewModel viewModel, FormCollection all, string hvalue, HttpPostedFileBase PostedNRLFile)
始终为空,请在这方面帮助我.
Which is always null, Please help me in this.
我已经尝试过一些操作,例如在viewmodel中创建属性,这也是空的.
在我的beginform标记中也使用了new { enctype = "multipart/form-data", id = "documentForm" }
.
还检查了它们只是源中的一个标签.
I have already tried few things like creating a property in viewmodel, this is also null.
Also used new { enctype = "multipart/form-data", id = "documentForm" }
in my beginform tag.
Also checked their is only one tag in source.
推荐答案
您必须在表单标签中添加enctype = "multipart/form-data"
.
You have to add enctype = "multipart/form-data"
in form tag.
@using (Html.BeginForm("Action", "Controller", FormMethod.Post,
new { enctype = "multipart/form-data" }))
在控制器操作中尝试此c#代码
Try this c# code in controller action
if (Request.Files != null && Request.Files.Count > 0)
{
HttpPostedFileBase file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
}
}
您必须检索文件.
这篇关于HTTPPostedFileBase总是在控制器中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!