MVC:使用HttpPostedFileBase返回NULL

时间:2015-10-09 11:31:10

标签: model-view-controller httppostedfilebase

我试图包含文件上传,它始终保持为NULL。这是我的模型:

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

我的观点: 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("")

控制器:

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

model.PathwaysToImpactUploadDocument 始终为NULL。我错过了什么?

1 个答案:

答案 0 :(得分:2)

这里有几件事似乎令人怀疑:

  1. 您的视图中似乎没有表单标记或提交按钮(可能是也可能不是问题)
  2. 您的视图模型不是您的操作方法的模型
  3. 我会确保你的观点看起来像:

    @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>
    }