繁体   English   中英

部分视图MVC上的文件HttpPostedFileBase上传帖子NULL

[英]File HttpPostedFileBase upload post NULL on partial view 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
}

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

<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以匹配属性名称,您将收到它们,但是如果任何文件输入为空,它们将不符合您的模型)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM