HttpPostedFileBase不具约束力的模型 [英] HttpPostedFileBase not binding to model
本文介绍了HttpPostedFileBase不具约束力的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的ViewModel
公共类FaultTypeViewModel
{
[HiddenInput(DisplayValue = FALSE)]
公众诠释TYPEID {搞定;组; } [必填(ErrorMessageResourceType = typeof运算(AdministrationStrings),ErrorMessageResourceName =FaultTypeNameRequired)]
[显示(=的ResourceType typeof运算(AdministrationStrings),名称=FaultTypeName)]
公共字符串类型名{获得;组; } [显示(=的ResourceType typeof运算(AdministrationStrings),名称=FaultTypeDescription)]
[数据类型(DataType.MultilineText)
公共字符串TypeDescription {搞定;组; } [显示(=的ResourceType typeof运算(AdministrationStrings),名称=FaultTypeImageFile)]
公共HttpPostedFileBase TypeImageFile {搞定;组; } [HiddenInput(DisplayValue = FALSE)]
公共字符串TypeImageURL {搞定;组; }
}
通知我有一个TypeImageFileHttpPostedFileBase
我预计到模型传递到控制器BU我只是不断收到空模型绑定会纽带,财产的形式。
下面是在查看相关code:
@using(Html.BeginForm(AddFaultType,管理,FormMethod.Post))
{ < DIV CLASS =模头>
<按钮式=按钮级=关闭数据解雇=莫代尔ARIA隐藏=真>
×< /按钮>
< H3 ID =myModalLabel> @ SharedStrings.Add @ SharedStrings.FaultType< / H3 GT&;
< / DIV>
< DIV CLASS =模体>
@ Html.ValidationSummary(真)
< DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.TypeName)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.TypeName)
@ Html.ValidationMessageFor(型号=> model.TypeName)
< / DIV>
< DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.TypeDescription)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.TypeDescription)
@ Html.ValidationMessageFor(型号=> model.TypeDescription)
< / DIV>
< DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.TypeImageFile)
< / DIV>
< DIV CLASS =主编场>
<输入类型=文件名称=TypeImageFileID =TypeImageFile/>
< / DIV>
< / DIV> < DIV CLASS =模式躯>
<输入类型=提交值=@ SharedStrings.Add级=BTN BTN-主要/>
@ Html.ActionLink(SharedStrings.Cancel,索引,管理,空,新的{类=BTN,data_dismiss =莫代尔,aria_hidden =真})
< / DIV>
}
和这里是控制器:
[HttpPost]
公众的ActionResult AddFaultType(FaultTypeViewModel i_FaultToAdd)
{
变种文件名= Path.GetFileName(i_FaultToAdd.TypeImageFile.FileName);
VAR路径= Path.Combine(使用Server.Mappath(〜/ App_Data文件/上传),文件名);
i_FaultToAdd.TypeImageFile.SaveAs(路径); 返回RedirectToAction(「指数」);
}
解决方案
请确保你设置你的窗体上的是enctype
属性多部分/ form-data的
表单上,如果你希望能够上传文件:
@using(Html.BeginForm(AddFaultType,管理,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
...
}
here is my ViewModel
public class FaultTypeViewModel
{
[HiddenInput(DisplayValue = false)]
public int TypeID { get; set; }
[Required(ErrorMessageResourceType = typeof(AdministrationStrings), ErrorMessageResourceName = "FaultTypeNameRequired")]
[Display(ResourceType = typeof(AdministrationStrings), Name = "FaultTypeName")]
public string TypeName { get; set; }
[Display(ResourceType = typeof(AdministrationStrings), Name = "FaultTypeDescription")]
[DataType(DataType.MultilineText)]
public string TypeDescription { get; set; }
[Display(ResourceType = typeof(AdministrationStrings), Name = "FaultTypeImageFile")]
public HttpPostedFileBase TypeImageFile { get; set; }
[HiddenInput(DisplayValue = false)]
public string TypeImageURL { get; set; }
}
Notice I have a "TypeImageFile" HttpPostedFileBase I would expect that the model binder would bond that property from the form to the model passes to the controller bu I just keep receiving null.
here is the relevant code in the View:
@using (Html.BeginForm("AddFaultType","Administration", FormMethod.Post))
{
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×</button>
<h3 id="myModalLabel">@SharedStrings.Add @SharedStrings.FaultType</h3>
</div>
<div class="modal-body">
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.TypeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TypeName)
@Html.ValidationMessageFor(model => model.TypeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TypeDescription)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TypeDescription)
@Html.ValidationMessageFor(model => model.TypeDescription)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TypeImageFile)
</div>
<div class="editor-field">
<input type="file" name="TypeImageFile" id="TypeImageFile" />
</div>
</div>
<div class="modal-footer">
<input type="submit" value="@SharedStrings.Add" class="btn btn-primary" />
@Html.ActionLink(SharedStrings.Cancel, "Index", "Administration", null, new { Class = "btn", data_dismiss = "modal", aria_hidden = "true" })
</div>
}
and here is the controller:
[HttpPost]
public ActionResult AddFaultType(FaultTypeViewModel i_FaultToAdd)
{
var fileName = Path.GetFileName(i_FaultToAdd.TypeImageFile.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
i_FaultToAdd.TypeImageFile.SaveAs(path);
return RedirectToAction("Index");
}
解决方案
Make sure you've set the enctype
attribute on your form to multipart/form-data
on your form if you want to be able to upload files:
@using (Html.BeginForm("AddFaultType", "Administration", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
...
}
这篇关于HttpPostedFileBase不具约束力的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文