Pass a model object while using a upload
- by Dejan.S
I'm trying to pass my model object along with the file I'm uploading but I'm stuck on how I should that should be done. This is the code I use now
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(PageBody pageBody)
{
foreach (string file in Request.Files)
{
var hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Content/Uploads/",
Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
_pageBody.Pictures.Add(new PageBodyPicture() { Picture = file });
}
return View(pageBody);
}
here is my view code, I got the model.Id but there but it wont pass even if I put just Id to the Upload method.
<form action="/Admin/Upload" enctype="multipart/form-data" method="post">
<%= Html.HiddenFor(model => model.Id)%>
<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>