【问题标题】:HttpPostedFileBase not binding to modelHttpPostedFileBase 未绑定到模型
【发布时间】:2013-02-08 23:36:53
【问题描述】:

这是我的视图模型

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。

这是视图中的相关代码:

@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>
}

这里是控制器:

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

【问题讨论】:

  • 快乐的普珥节。请在您的视图中包含您的表单。
  • @DaveA 谢谢戴夫! :) ... 祝你普珥节快乐,我已经添加了包括表单在内的完整视图代码。

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


【解决方案1】:

如果您希望能够上传文件,请确保您已将表单上的 enctype 属性设置为 multipart/form-data

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

【讨论】:

  • 这行得通。确保输入名称与视图模型属性匹配,并且输入类型为 [type="file"]
【解决方案2】:

完成达林的回答: 如果您希望能够上传文件,请确保您已将表单上的 enctype 属性设置为 multipart/form-data

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

为确保您的 &lt;input&gt; 作为模型的一部分传输到控制器,请使用 Idname 的 Html 帮助程序,如下所示:

<input type="file" id="@Html.IdFor(x=>x.HttpPostedFileBase)" name="@Html.NameFor(x=>x.HttpPostedFileBase)" accept=".csv,.txt"/>

在 MVC5 中工作对不起,我找不到任何关于 MVC3 中可用帮助器的参考

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多