Search code examples
asp.net-mvchttphttppostedfilebase

mvc pass file with model to controller post


Is it possible to attach HttpPostedFileBase to your model on post to a controller?

I was able to just pass only HttpPostedFileBase without model.

In the network tab in my browser I see file is getting attached but when it gets to my controller it's null

[HttpPost]
public ActionResult ProcessFile(UploadMopdelWrapper model)
{

}

and

public class UploadMopdelWrapper
{
    public UploadMopdelWrapper()
    {

    }

    public UploadMopdelWrapper(UploadMopdel upload)
    {
        UploadModel = upload;
    }
    public HttpPostedFileBase SelectedFile { get; set; }
    public UploadMopdel UploadModel { get; set; }
    public IEnumerable<ExcelRecord> ExcelList { get; set; }
}

Here is the view

@using (@Html.BeginForm("ProcessFile", "Hcp", new { enctype = "multipart/form-data" }))
{
    <div class="form-group">
        <div class="col-md-4 text-right">
            @Html.LabelFor(x => x.UploadModel.JobNumber, new { @class = "control-label" })
        </div>
        <div class="col-md-8">
            @Html.TextBoxFor(x => x.UploadModel.JobNumber, new { @class = "form-control" })
            @Html.ValidationMessageFor(x=>x.UploadModel.JobNumber, null, new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-4 text-right">
            @Html.LabelFor(x => x.UploadModel.JobName, new { @class = "control-label" })
        </div>
        <div class="col-md-8">
            @Html.TextBoxFor(x => x.UploadModel.JobName, new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.UploadModel.JobName, null, new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-4 text-right">
            @Html.LabelFor(x => x.UploadModel.NotificationEmail, new { @class = "control-label" })
        </div>
        <div class="col-md-8">
            @Html.TextBoxFor(x => x.UploadModel.NotificationEmail, new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.UploadModel.NotificationEmail, null, new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-4 text-right">
            <label class="control-label">Selkect a File:</label>
        </div>
        <div class="col-md-8">
            <input type="file" name="selectedFile" />
        </div>
    </div>
    <div class="col-md-offset-4 col-md-8">
        <input type="submit" class="btn btn-info btn-block col-md-12" />
    </div>

}

In my network tab I see this:

UploadModel.JobNumber:1
UploadModel.JobName:w
UploadModel.NotificationEmail:w
selectedFile:week7_homework.sql

Solution

  • you have to set form method as Post like

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

    because else it will consider new { enctype = "multipart/form-data" } as route value. If you look at the overloads of BeginForm you are using

    FormExtensions.BeginForm Method (HtmlHelper, String, String, Object)