I have controller like this :
public ActionResult SaveBook(Book coll, HttpPostedFileBase EBook)
{
}
and view like the following:
@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>
}
My Model like this:
[Required(ErrorMessage = "Title Required")]
public String Title { get; set; }
[Required(ErrorMessage = "ISBN Required")]
public String ISBN { get; set; }
public HttpPostedFileBase EBook { get; set; }
But still I am getting value fro EBook "null" value in "EBook" object or in the "coll" object of the Controller. Any suggestion?
Since you have same name in your model, you are getting it null. happened to me yesterday as well rename one of them to something else.
Try following:
<div class="editor-field">
<input type="file" name="file" />
</div>
Similarlar you action should be
public ActionResult SaveBook(Book coll, HttpPostedFileBase file)
{
}
EBook is already a property of your Model. Change your Action to the following for the model binding to work properly:
[HttpPost]
public ActionResult SaveBook(Book coll)
{
}
The fact I found now. I increase maxRequestLength like:
<httpRuntime targetFramework="4.5" maxRequestLength="1048576" />
and it worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.