【发布时间】: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 => m.Image, new { type = "file" })将生成正确的 html
标签: asp.net-mvc