【问题标题】:Upload a HttpPostedFileBase for image but always null为图像上传 HttpPostedFileBase 但始终为空
【发布时间】:2016-05-01 20:35:51
【问题描述】:

我是 MVC 和 Web 编程的新手。

我的上传图片工作正常,但是当我使用几乎完全相同的代码来允许用户修改其上传时,它的 HttpPostedFileBase 始终为空。快把我逼疯了……

这是模型 公共类 ModifyGenreViewModel { 公共 int ID { 获取;放; }

        [Display(Name = "Nom du style")]
        public string Name { get; set; }

        [Display(Name = "Image")]
        public HttpPostedFileBase Image { get; set; }
    }

还有风景

@using (Html.BeginForm("ModifyGenre", "Upload", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()
            <div class="form-horizontal">

                <h4>@Resource.StyleCreationHeader</h4>

                <hr />
                @Html.ValidationMessageFor(model=>Model)
                <div class="form-group" style="display:none">

                    @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-5">
                        @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
                    </div>
                </div>

                <div class="form-group">

                    @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-5">
                        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.Image, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        <input type="file" data-val="true" id="ModifyGenreViewModel_Image" name="ModifyGenreViewModel.Image" />
                        @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="@Resource.Modify" class="btn btn-primary" />
                    </div>
                </div>
            </div>
        }

当我在控制器中设置断点时,我看到了 Id 和名称,但 Image 始终为空。

感谢您的帮助!

【问题讨论】:

  • @Html.TextBoxFor(m =&gt; m.Image, new { type = "file" }) 将生成正确的 html

标签: asp.net-mvc


【解决方案1】:

文件输入的名称属性值应与视图模型属性名称匹配,以便模型绑定正常工作。

将输入字段名称改为"Image"

<input type="file" data-val="true" id="ModifyGenreViewModel_Image" name="Image" />

假设您的 HttpPost 操作方法接受 ModifyGenreViewModel 对象作为参数。

[HttpPost]
public ActionResult ModifyGenre(ModifyGenreViewModel  model)
{
  // to do : return something
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2013-03-02
    相关资源
    最近更新 更多