0

I am trying to include file upload by it remains NULL all the time. Here is my Model:

   [Display(Name = "Upload Document")]
    [DataType(DataType.Upload), FileSize(102400)]
    public HttpPostedFileBase PathwaysToImpactUploadDocument { get; set; }

My View: ProjectForm.cshtml

  @Html.EditorFor(m => m.PathwaysToImpactUploadDocument)

Upload.cshtml

@model HttpPostedFileBase

@{
    Layout = "~/Views/Shared/EditorTemplates/_Layout.cshtml";
}
@Html.TextBox("", null, new {@class = "form-control", type = "file"})
@Html.ValidationMessage("")

Controller:

        [HttpPost]
        public ActionResult New(ProjectCreateViewModel model){
}

But the model.PathwaysToImpactUploadDocument is always NULL. What am I missing?

JADE
  • 475
  • 4
  • 12
  • 24
  • 1
    How are you uploading the file - standard submit (have you included the `enctype` attribute in the form) or ajax? –  Oct 09 '15 at 12:24
  • I needed to add the enctype, you are correct. :) – JADE Oct 12 '15 at 02:19

1 Answers1

2

There are a couple of things that seem suspect here:

  1. it appears that there is no form tag or submit button (may or may not be a problem) in your view
  2. The model for your view is not the model for your action method

I would make sure your view looks something like:

@model ProjectCreateViewModel
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
    @Html.TextBoxFor(m => m.file, new {@class = "form-control", type = "file"})
    @Html.ValidationMessageFor(m => m.file)
    <input type="submit">Upload</input>
}
Ben
  • 1,032
  • 8
  • 18
  • I double checked and found out that the view is the problem, it does not have enctype = "multipart/form-data" declared. Thank you! I can now move forward to the logic of saving and downloading the file. – JADE Oct 12 '15 at 02:19