小编Una*_*avi的帖子

使用MVC上传文件HttpPostedFileBase == null

我的控制器

public ActionResult Edit(int id)
{
    return this.EditDefault(id);
}

[HttpPost]
public ActionResult Edit(int id, Models.Company model)
{
    return this.EditDefault(id, model);
}
Run Code Online (Sandbox Code Playgroud)

我的模特

pulbic class Company
{
    ... Many other Propeties
    public HttpPostedFileBase File { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的看法

@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
{
    ... Many other Properties
    @Html.TextBoxFor(m => m.File, new
    {
        type = "file", style = "display:none" 
    })
    ... Submit
}
Run Code Online (Sandbox Code Playgroud)

所以现在我的问题是,当我提交页面时,模型中的信息是正确的,但文件属性仍为空.

我找到了一些解决方案,人们在控制器中添加了HttpPostedFileBase作为参数(试过它也不起作用),但我想避免这种情况,因为模型和控制器是用T4生成的.那么有人知道为什么File Property总是为null吗?

一些帮助真的很高兴:)

更新:找到解决方案thx到Matt Tabor.

对我来说,解决方案看起来像这样,因为我使用共享的编辑页面.javascript部分是隐藏实际的文件上传元素并使用跨度,因为文件上传不是样式的.

//Shared Part
@{
    RouteData routeData = …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc file-upload

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

asp.net-mvc ×1

file-upload ×1