Download MVC file HttpPostedFileBase == null

My controller

public ActionResult Edit(int id)
{
    return this.EditDefault(id);
}

[HttpPost]
public ActionResult Edit(int id, Models.Company model)
{
    return this.EditDefault(id, model);
}

My model

pulbic class Company
{
    ... Many other Propeties
    public HttpPostedFileBase File { get; set; }
}

My view

@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
{
    ... Many other Properties
    @Html.TextBoxFor(m => m.File, new
    {
        type = "file", style = "display:none" 
    })
    ... Submit
}

So now my problem is that when I submit the page, the information in the model is correct, but the file property is still zero.

I found some solutions in which people added HttpPostedFileBase as a parameter in the controller (I tried it also does not work), but I would like to avoid this, since the model and controller are generated using T4. So is there someone why the file property is always null?

Would be very happy for some help :)

Update: Find a thanks solution for Matt Tabor.

, . javascript , , .

//Shared Part
@{
    RouteData routeData = this.ViewContext.RouteData;
    string currentController = routeData.GetRequiredString("controller");
}

@using (Html.BeginForm("Edit", currentController, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    //Special Part
    ... Many other Properties
    //File upload which is hidden
    @Html.TextBoxFor(m => m.File, new
    {
        type = "file", style = "display:none" 
    })
    //Span which forwards the clicks to the file upload
    <span id="fake-file-name">Kein Bild</span>
    ... Submit
}

<script type="text/javascript">
    $(function () {
        //forward the click from the span to the file upload
        $("#fake-file-name").click(function () {
            $("#File").click();
        });
        //display the chosen file name to the user with the styled span
        $("#File").bind('change', function () {
            //we don't want the C:\fakepath to show
            var displayFileName = this.value.replace("C:\\fakepath\\", "");
            $("#fake-file-name").text(displayFileName);
        });
    });
</script>
+3
2

@using (Html.BeginForm("Edit", "CONTROLLER", null,FormMethod.Post, new { enctype =   "multipart/form-data" }))
+6
@Html.TextBoxFor(m => m.File, new
{
    type = "file", style = "display:none" 
})

, -

<input type="file" name="File" id="File"/>

PS: .

UPDATE : none , .

+3

All Articles