我有这样的控制器:
public ActionResult SaveBook(Book coll, HttpPostedFileBase EBook)
{
}视图如下所示:
@using (Html.BeginForm("SaveBook", "Book", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Book</legend>
<table>
<tr>
<td>@Html.LabelFor(model => model.Title)</td>
<td>@Html.EditorFor(model => model.Title)@Html.ValidationMessageFor(model => model.Title)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.ISBN)</td>
<td>@Html.EditorFor(model => model.ISBN)@Html.ValidationMessageFor(model => model.ISBN)</td>
</tr>
<tr><td>@Html.LabelFor(model => model.EBook) </td><td>@Html.TextBoxFor(model => model.EBook, new { type = "file", accept = ".pdf" })
@Html.ValidationMessageFor(model => model.EBook)</td></tr>
</table>
<p>
<input type="submit" value="SaveBook" />
</p>
</fieldset>
}我的模型如下:
[Required(ErrorMessage = "Title Required")]
public String Title { get; set; }
[Required(ErrorMessage = "ISBN Required")]
public String ISBN { get; set; }
public HttpPostedFileBase EBook { get; set; }但我仍然从" EBook“对象或控制器的”EBook“对象中的coll "null”值中获得值。有什么建议吗?
发布于 2013-09-11 17:59:13
由于您的模型中具有相同的名称,因此您将获得null。昨天在我身上也发生了同样的事情,把他们中的一个重新命名为其他名称。
尝试以下操作:
<div class="editor-field">
<input type="file" name="file" />
</div>你的行动应该是类似的
public ActionResult SaveBook(Book coll, HttpPostedFileBase file)
{
}发布于 2013-09-11 18:22:05
EBook已经是您的模型的一个属性。将您的操作更改为以下内容,以使模型绑定正常工作:
[HttpPost]
public ActionResult SaveBook(Book coll)
{
}发布于 2013-09-11 22:44:33
我现在发现的事实。我像这样增加maxRequestLength:
<httpRuntime targetFramework="4.5" maxRequestLength="1048576" />而且它起作用了。
https://stackoverflow.com/questions/18734699
复制相似问题