asp.net-mvc – HttpPostedFileBase没有绑定到模型
发布时间:2020-05-24 05:27:57 所属栏目:asp.Net 来源:互联网
导读:这是我的ViewModel public class FaultTypeViewModel{ [HiddenInput(DisplayValue = false)] public int TypeID { get; set; } [Required(ErrorMessageResourceType = typeof(AdministrationStrings), Err
|
这是我的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; }
}
注意我有一个“TypeImageFile”HttpPostedFileBase 这里是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",null,new { Class = "btn",data_dismiss = "modal",aria_hidden = "true" })
</div>
}
这里是控制器: [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");
}
解决方法如果您希望能够上传文件,请确保在表单上设置了enctype属性,以便在表单上进行多部分/表单数据:@using (Html.BeginForm("AddFaultType",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
...
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在Umbraco 6.1.1 MVC 4中,如何使用继承自Um
- asp.net-core – CoreCLR中的哈希算法
- asp.net-mvc – 如何在ASP.NET MVC中使用编译的全局资源
- .net – 如何更改乌节纪录库
- 如何在ASP.Net WebControl的“Content”内部属性中包含其他
- asp.net – 在Web应用程序中排队长时间运行的任务
- asp.net 文章内容分页显示的代码
- asp.net – 与Entity Framework中的联结表有多对多的关系?
- asp.net-mvc-4 – MVC4最小参考
- asp.net-mvc – Durandal和ASP.NET MVC约定
