微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – HttpPostedFileBase没有绑定到模型

这是我的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
我会期望模型绑定器将该属性从表单绑定到模型传递给控制器​​我只是继续接收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",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" }))
{
    ...
}

原文地址:https://www.jb51.cc/aspnet/249823.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐