2016-11-19 68 views
0

我正在处理多个文件,我正在使用HttpPostedFileBase文件HttpPostedFileBase上传文章NULL在部分视图上MVC

提交表格时,file属性为null

型号

public partial class Driver 
{ 
    public int DriverId { get; set; } 
    public string DriverFirstName { get; set; } 
    .... 
    public List<DriverImages> DriverImages { get; set; } 
} 

public class DriverImages 
{ 
    public int DriverImageID { get; set; } 
    public string ImagePath { get; set; } 
    public string Description { get; set; } 
    public HttpPostedFileBase file { get; set; } 
} 

局部视图

@model DataModel.DriverImages 
... 
@using (Design.Common.HtmlPrefixScopeExtensions.BeginCollectionItem(this.Html, "DriverImages")) 
{ 
    <table> 
     <tr> 
      <td> 
       @Html.TextBoxFor(m => Model.Description) 
      </td> 
      <td> 
       <input type="file" name="file" /> 
      </td> 
     </tr> 
    </table> 
} 

控制器

[httppost] 
public ActionResult Create(Driver ObjDriverModel, HttpPostedFileBase[] files) 
{ 
    // ObjDriverModel.DriverImages.file // always null 
} 

回答

1

你不产生你的模型属性文件输入。它需要

<td> 
    @Html.TextBoxFor(m => m.Description) 
</td> 
<td> 
    @Html.TextBoxFor(m => m.file, new { @type = "file" }) 
</td> 

这将产生

<input type="file" name="DriverImages[xxx].file" .... /> 

其中xxxGuid

,并从你的方法删除HttpPostedFileBase[] file参数(注意,您会收到他们有重命名的参数到file以匹配属性名称,但如果任何文件输入为空,则它们不会匹配到您的模型)。

+0

伟大的帮助...它现在发布我的文件..谢谢很多 –