views:

57

answers:

2

Ok, I've been going at this for several hours and I simply cannot find the solution.

I want to get some data from my user. So first, I use a controller to create a view which receives a Model:

public ViewResult CreateArticle()
{
    Article newArticle = new Article();
    ImagesUploadModel dataFromUser = new ImagesUploadModel(newArticle);
    return View(dataFromUser);
}

Then, I have the view:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">

    <h2>AddArticle</h2>

    <% using (Html.BeginForm("CreateArticle", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })){ %>


                <%= Html.LabelFor(model => model.newArticle.Title)%>
                <%= Html.TextBoxFor(model => model.newArticle.Title)%>

                <%= Html.LabelFor(model => model.newArticle.ContentText)%>
                <%= Html.TextBoxFor(model => model.newArticle.ContentText)%>

                <%= Html.LabelFor(model => model.newArticle.CategoryID)%>
                <%= Html.TextBoxFor(model => model.newArticle.CategoryID)%>

                <p>
                    Image1: <input type="file" name="file1" id="file1" />
                </p>
                <p>
                    Image2: <input type="file" name="file2" id="file2" />
                </p>

            <div>
                <button type="submit" />Create
            </div>



    <%} %>


</asp:Content>

and finally - the original controller, but this time configured to accept the data:

   [HttpPost]
    public ActionResult CreateArticle(ImagesUploadModel dataFromUser)
    {
        if (ModelState.IsValid)
        {
            HttpPostedFileBase[] imagesArr;
            imagesArr = new HttpPostedFileBase[2]; 
            int i = 0;
            foreach (string f in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[f];
                if (file.ContentLength > 0)
                    imagesArr[i] = file;
            }

The rest of this controller does not matter since no matter what I do, the count attribute of Request.Files (or Request.Files.Keys) remains 0. I simply can't find a way to pass the files from the form (the Model passes just fine).

+2  A: 

You might want to consider not posting the files with the rest of the form- there are good reasons and other ways you can achieve what you want.

Also, check out this question and this advice regarding file uploads in MVC.

cottsak
Well, I guess I'll just split it up into 2 views then :-Pthanks.
Tom Teman
It could even be the same view with two forms in it (another way to achieve the "2 step process").
cottsak
A: 

You could add the files to your view model:

public class ImagesUploadModel
{
    ...
    public HttpPostedFileBase File1 { get; set; }
    public HttpPostedFileBase File2 { get; set; }
}

And then:

[HttpPost]
public ActionResult CreateArticle(ImagesUploadModel dataFromUser)
{
    if (ModelState.IsValid)
    {
        // Use dataFromUser.File1 and dataFromUser.File2 directly here
    }
    return RedirectToAction("index");
}
Darin Dimitrov
that would be an awesome solution, but how do I update the model data in the view. that is - how does the upload mechanism work (what replaces <input type="file" name="file1" id="file1" /> ?)
Tom Teman
There's no HTML helper built into ASP.NET MVC currently that allows you to generate file inputs. You need to generate them manually or use one of the helpers in MVCContrib.
Darin Dimitrov
Well, I guess I'll just split it up into 2 views then :-Pthanks.
Tom Teman