ASP.NET MVC passing Model *together* with files back to controller
Asked Answered
C

2

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).

Carboni answered 6/10, 2010 at 22:41 Comment(1)
possible duplicate of ASP.NET MVC posted file model binding when parameter is ModelBois
I
3

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.

Ician answered 7/10, 2010 at 2:53 Comment(2)
Well, I guess I'll just split it up into 2 views then :-P thanks.Carboni
It could even be the same view with two forms in it (another way to achieve the "2 step process").Ician
G
3

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");
}
Goosefish answered 7/10, 2010 at 6:25 Comment(5)
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" /> ?)Carboni
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.Goosefish
Well, I guess I'll just split it up into 2 views then :-P thanks.Carboni
@DarinDimitrov Got error:- No MediaTypeFormatter is available to read an object of type 'CommentModel' from content with media type 'multipart/form-data'Dachia
@ujjaval, you seem to be attempting to use this in a Web API controller action. My answer is about ASP.NET MVC.Goosefish

© 2022 - 2024 — McMap. All rights reserved.