文件HttpPostedFileBase在部分视图MVC上上传NULL

时间:2016-11-19 07:25:12

标签: c# asp.net-mvc

我正在处理多个文件帖子,我正在使用HttpPostedFileBase

提交表单时,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 个答案:

答案 0 :(得分:1)

您没有为模型属性生成文件输入。它必须是

import java.util.InputMismatchException;
import java.util.Scanner;

class AddNumbers
{
public static void main(String args[])
  {
    double x, y, z;
    System.out.println("Enter two double to calculate their sum ");
    Scanner in = new Scanner(System.in);

    while (true) {
        try {
            x = Double.parseDouble(in.nextLine());
            y = Double.parseDouble(in.nextLine());
            z = x + y;
            System.out.println("Sum of entered double = " + z);
            break;
        } catch (Exception e) {
            System.out.println("Not a double param, please enter again");
            continue;
        }
    }

}

这将生成

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

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

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