0

So I've read several guides on this and I'm still having a problem. I'm using ASP.NET MVC 4 Razor.

Here's what I have so far:

Controller

public class FormsController : Controller
{
    [HttpPost]
    [ValidateAntiForgeryToken]
    [AllowAnonymous]
    public ActionResult TechProjectPlan(IEnumerable<HttpPostedFileBase> files, MvcApplication1.Models.TechProjectPlanModel model, string returnUrl)
    {
        //stuff here
    }
}

This form includes a lot of different fields, not just the files, so I have the model here as well. The request gets to the controller and action just fine, but "files" is always "Count = 0", just empty.

Here's the View stuff:

@using (Html.BeginForm("SubmitForm", "Forms", new { enctype = @"multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <h3>Project Information</h3>
    <fieldset>
    <legend>Project Information</legend>
    <ol style="display:block; *display:inline; *zoom:1;">
        <li style="float:left;">
            @Html.LabelFor(m => m.Item1)
            @Html.TextBoxFor(m => m.Item1, new { @class = "CostItem" })

        </li>
        <li style="float:left;">
            @Html.LabelFor(m => m.Quantity1)
            @Html.TextBoxFor(m => m.Quantity1, new { @class = "CostQuantity" })
            <span>  $</span>
        </li>
        <li style="float:left;">
            @Html.LabelFor(m => m.PerUnitCost1)
            @Html.TextBoxFor(m => m.PerUnitCost1, new { @class = "CostPerUnitCost" })
            <span>  $</span>
        </li>
        <li style="float:left;">
            @Html.LabelFor(m => m.Extension1)
            @Html.TextBoxFor(m => m.Extension1, new { @class = "CostExtension" })
        </li>
        <li style="float:left;">
            @Html.LabelFor(m => m.Attachment1)
            <input type="file" value="Upload" name="file" id="upload1" style="padding: 5px;" />
        </li>
    </ol>
    <ol style="display:block; *display:inline; *zoom:1;">
        <li style="float:left;">
            @Html.TextBoxFor(m => m.Item2, new { @class = "CostItem" })
        </li>
        <li style="float:left;">
            @Html.TextBoxFor(m => m.Quantity2, new { @class = "CostQuantity" })
            <span>  $</span>
        </li>
        <li style="float:left;">
            @Html.TextBoxFor(m => m.PerUnitCost2, new { @class = "CostPerUnitCost" })
            <span>  $</span>
        </li>
        <li style="float:left;">
            @Html.TextBoxFor(m => m.Extension2, new { @class = "CostExtension" })
        </li>
        <li style="float:left;">
            <input type="file" value="Upload" name="files" id="upload2" style="padding: 5px;" />
        </li>
    </ol>
    </fieldset>
    <input type="submit" value="Submit"/>
}

As you can see, the input types file all have the name "files", matching the parameter in the ActionResult on the controller, and it's my understanding that the compiler is supposed to implicitly match these.

Thanks for the help!

1 Answer 1

1

Try changing the name of the file input control from 'file' to 'files[0]'

<input type="file" value="Upload" name="files[0]" id="upload1" style="padding: 5px;" />

similarly for other input controls make sure it goes like files[1],files[2] etc and the name of the argument in action method is 'files' i.e. if your argument name is 'arg' then name of your file input control should be arg[0],arg[1] etc

Also make sure the form tag has enctype="multipart/form-data"

<form action="<ActionName>"  method="post" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

2 Comments

Ok so that is causing the files to come through kind of. Now it's showing the correct number of files in the enumerable, but each of the files in the enumerable are null. I have noticed that if I look at the page source, Html.BeginForm is outputting:<form action="/Forms/SubmitForm?enctype=multipart%2Fform-data" method="post">. Is it supposed to convert the "/" into %2F?
Ok I got it, what I did was left out the FormMethod parameter in BeginForm, so it was doing weird stuff. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.