asp.net-mvc - HttpPostedFileBase 未绑定(bind)到模型

标签 asp.net-mvc asp.net-mvc-3 model-binding

这是我的 View 模型

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
我希望模型绑定(bind)器将该属性从表单绑定(bind)到模型传递给 Controller ​​,但我只是不断收到 null。

这是 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>
}

这是 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");
        }

最佳答案

确保您已设置 enctype在您的表格上注明 multipart/form-data如果您希望能够上传文件,请在您的表单上:

@using (Html.BeginForm("AddFaultType", "Administration", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    ...
}

关于asp.net-mvc - HttpPostedFileBase 未绑定(bind)到模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15039648/

相关文章:

c# - 通过 Web API 使用 Entity Framework 进行分页

c# - 将空字符串绑定(bind)到 Guid.Empty 或避免模型状态错误

asp.net-mvc - 更改每个 Controller 的模型绑定(bind)器而不是按类型或属性?

javascript - 在 asp.net MVC 中过滤 ListView 的最佳方法

c# - Entity Framework : after SaveChanges returned object doesn't have children

asp.net - NodaTime 转换(第 2 部分)。如何?

c# - 将 JSON 绑定(bind)到模型时使 WebAPI 拒绝未知字段

asp.net-mvc - Pushsharp 连接失败

c# - MVC 4、EF 5 SaveChanges 不更新数据库

javascript - 使用数据从 jQuery 重定向