Asp.net mvc 上载MVC HttpPostedFileBase==null的文件
我的控制器Asp.net mvc 上载MVC HttpPostedFileBase==null的文件,asp.net-mvc,file-upload,Asp.net Mvc,File Upload,我的控制器 public ActionResult Edit(int id) { return this.EditDefault(id); } [HttpPost] public ActionResult Edit(int id, Models.Company model) { return this.EditDefault(id, model); } 我的模型 pulbic class Company { ... Many other Propeties p
public ActionResult Edit(int id)
{
return this.EditDefault(id);
}
[HttpPost]
public ActionResult Edit(int id, Models.Company model)
{
return this.EditDefault(id, model);
}
我的模型
pulbic class Company
{
... Many other Propeties
public HttpPostedFileBase File { get; set; }
}
我的看法
@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
{
... Many other Properties
@Html.TextBoxFor(m => m.File, new
{
type = "file", style = "display:none"
})
... Submit
}
所以我现在的问题是,当我提交页面时,模型中的信息是正确的,但是File属性仍然为null
我发现了一些解决方案,其中人们在控制器中添加了HttpPostedFileBase作为参数(尝试过了,但它也不起作用),但我还是希望避免这种情况,因为模型和控制器是用T4生成的。有人知道为什么文件属性总是空的吗
如果能得到一些帮助,我会非常高兴:)
更新:找到了Matt Tabor的thx解决方案
对我来说,解决方案是这样的,因为我使用了一个共享的编辑页面。
javascript部分是隐藏实际的file upload元素,并使用一个span,因为文件上传不支持样式
//Shared Part
@{
RouteData routeData = this.ViewContext.RouteData;
string currentController = routeData.GetRequiredString("controller");
}
@using (Html.BeginForm("Edit", currentController, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
//Special Part
... Many other Properties
//File upload which is hidden
@Html.TextBoxFor(m => m.File, new
{
type = "file", style = "display:none"
})
//Span which forwards the clicks to the file upload
<span id="fake-file-name">Kein Bild</span>
... Submit
}
<script type="text/javascript">
$(function () {
//forward the click from the span to the file upload
$("#fake-file-name").click(function () {
$("#File").click();
});
//display the chosen file name to the user with the styled span
$("#File").bind('change', function () {
//we don't want the C:\fakepath to show
var displayFileName = this.value.replace("C:\\fakepath\\", "");
$("#fake-file-name").text(displayFileName);
});
});
</script>
//共享部分
@{
RouteData RouteData=this.ViewContext.RouteData;
string currentController=RoutedData.GetRequiredString(“控制器”);
}
@使用(Html.BeginForm(“Edit”,currentController,null,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
//特殊部分
…许多其他财产
//文件上传是隐藏的
@Html.TextBoxFor(m=>m.File,新建)
{
type=“file”,style=“display:none”
})
//将单击转发到文件上载的范围
基恩·比尔德
提交
}
$(函数(){
//将单击从范围转发到文件上载
$(“#假文件名”)。单击(函数(){
$(“#文件”)。单击();
});
//向具有样式范围的用户显示所选文件名
$(“#文件”).bind('change',function(){
//我们不希望C:\fakepath显示
var displayFileName=this.value.replace(“C:\\fakepath\\”,“”);
$(“#假文件名”).text(显示文件名);
});
});
而是使用如下所示的输入类型文件-
<input type="file" name="File" id="File"/>
PS:名称应与模型属性名称匹配
更新
从代码中删除显示:无,应该可以正常工作。您需要将表单方法指定为post
@using (Html.BeginForm("Edit", "CONTROLLER", null,FormMethod.Post, new { enctype = "multipart/form-data" }))
thx,但试过了,不起作用。顺便说一句:@Html.TextBoxFor(m=>m.File,new{type=“File”,style=“display:none”})会生成这样的结果,所以几乎是一样的。当您删除display:none样式并尝试一下时会发生什么?
@using (Html.BeginForm("Edit", "CONTROLLER", null,FormMethod.Post, new { enctype = "multipart/form-data" }))