I have the following image upload code and controller. The hidden ID is POSTing successfully, but the image remains null.
The form:
@using(Html.BeginForm()){
<input type="hidden" name="merchandiseId" id="id" value="@ViewBag.Id"/>
<input type="file" name="image" id="image" />
<input type="submit" />
}
The controller:
[HttpPost]
public ActionResult AddImage(int merchandiseId, HttpPostedFileBase image)
Debugging and stepping in verifies that image is null while merchandiseId has the correct value.
You need to add the
enctype = "multipart/form-data"in your form. otherwise no file will be uploaded.An example