Pass a model object while using a upload

Posted by Dejan.S on Stack Overflow See other posts from Stack Overflow or by Dejan.S
Published on 2010-06-12T21:27:54Z Indexed on 2010/06/12 21:32 UTC
Read the original article Hit count: 233

Filed under:
|

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>

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc-2