如何解决文件输入返回空值ASP.NET MVC
我在包含文件输入的视图上有一个开始表单,但是当我尝试将输入值传递给控制器操作时,它将返回一个空值。
视图中的代码:
@using (Html.BeginForm("DoCreate","Admin",FormMethod.Post))
{
<label>Description:</label>
@Html.TextBox("Description",null,new { @class = "form-control",@type = "text",required = "required" });
<label>Price:</label>
@Html.TextBox("Price",@type = "number",required = "required" });
<label>Quantity:</label>
@Html.TextBox("Quantity",required = "required" });
<label>Image:</label>
@Html.TextBox("Image",@type = "file",required = "required" });
<label>Product Type:</label>
@Html.DropDownList("TypeID",new SelectList(Model,"ID","TypeName"),"Select Product Type",required = "required" });
<button class="btn btn-success" type="submit">Add to Table</button>
}
控制器中的代码
[HttpPost]
public ActionResult DoCreate(string Description,float Price,int Quantity,HttpPostedFileBase Image,int TypeID)
{
return View();
}
如果通过该操作插入一个断点,则所有其他变量都具有正确的值,但是Image返回null,我不确定为什么会这样,任何帮助将不胜感激。
解决方法
请检查
您只需要以htmlAttribute
格式添加new { enctype = "multipart/form-data"}
@using (Html.BeginForm("DoCreate","Admin",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
<label>Description:</label>
@Html.TextBox("Description",null,new { @class = "form-control",@type = "text",required = "required" });
<label>Price:</label>
@Html.TextBox("Price",@type = "number",required = "required" });
<label>Quantity:</label>
@Html.TextBox("Quantity",required = "required" });
<label>Image:</label>
@Html.TextBox("Image",@type = "file",required = "required" });
<label>Product Type:</label>
@Html.DropDownList("TypeID",new SelectList(Model,"ID","TypeName"),"Select Product Type",required = "required" });
<button class="btn btn-success" type="submit">Add to Table</button>
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。