HttpPostedFileBase always remaining null mvc

user2191421 picture user2191421 · Oct 17, 2013 · Viewed 8.3k times · Source

i know that there is a lot of questions like this, but i read them all and null of them solved my problem. Here below im going to show the code that i have in my view and my controller so that you can have an idea of my code.

View :
<td><% using (Html.BeginForm("Index", "Home", FormMethod.Post, new {enctype = "multipart/form-data"})) { %>
<%: Html.ValidationSummary(true) %>
                <div id="section1"> 
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Name) %>
    </div>
<label for="file">Filename:</label>
        <input type="file" name="myfile" id="File5" value="Choose File Banner1" />
     <p>&nbsp;</p>

And here is my controller:
[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(HomeModel model, string submitButton, HttpPostedFileBase myfile)
    {

I cant figure out what mite be the problem.. can someone help me please

Answer

Memet Olsen picture Memet Olsen · Dec 21, 2013

I also had the same problem and I ended up here while looking for an answer. I had forgotten the enctype parameter in the form definition

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
// form
}