HttpPostedFileBase 在使用 Model 发布时总是 returns null 但在没有模型的情况下工作 perfact
HttpPostedFileBase always returns null when posting it with Model but working perfact without model
我已经尝试了在 Whosebug 上找到的所有解决方案,但它们对我不起作用。我也尝试过 Tetsuya Yamamoto 解决方案,但当我将它与模型一起使用时,它仍然总是返回 null
。
更新
当我检查它时,我的文件类型输入保存数据,但在检查元素中它的值为 ""
两种方式我发布的文件都是这样的
@using (Html.BeginForm("AddLocation", "MasterData", FormMethod.Post, new { encytype = "multipart/form-data"}))
{
<div class="file-upload">
<input type="file" name="postedFile" />
</div>
//passing model value when using it
}
没有模型正常工作
public ActionResult AddLocation(HttpPostedFileBase file)
{
try
{
if (file != null) //Working Perfact
{
}
return View(model);
}
catch (Exception ex)
{
return View(model);
throw;
}
}
模型总是重新运行 null
public ActionResult AddLocation(LocationModel model, HttpPostedFileBase file)
{
try
{
if (ModelState.IsValid)
{
if (file != null) //Always return null when passing with model
{
}
}
return View(model);
}
catch (Exception ex)
{
return View(model);
throw;
}
}
您可以在模型中添加 HttpPostedFileBase 文件,它应该会获取该文件。
public class LocationModel
{
....
public HttpPostedFileBase File{ get; set; }
}
原因是多个 post 参数在 asp.net mvc
中不受支持
只需在视图模型 class 中添加 HttpPostedFileBase
属性 ,它与 <input type="file" />
元素具有相同的名称:
public class LocationModel
{
// other properties
public HttpPostedFileBase PostedFile { get; set; }
}
然后从控制器操作中删除第二个参数:
[HttpPost]
public ActionResult AddLocation(LocationModel model)
{
// do something
}
并用强类型绑定替换 <input>
文件元素:
@Html.TextBoxFor(model => model.PostedFile, new { type = "file" })
之后,您的文件应该在相应的视图模型上可用 属性。
旁注:
表单定义有错别字,应该使用enctype
而不是encytype
:
@model LocationModel
@using (Html.BeginForm("AddLocation", "MasterData", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="file-upload">
@Html.TextBoxFor(model => model.PostedFile, new { type = "file" })
</div>
}
参考:
mvc upload file with model - second parameter posted file is null
@ArunPratap 这个问题在 asp.net mvc 中相当有名,
实际上 POST 方法获取与视图中给定 ID 名称相同的文件,
由于表单数据是通过 headers 发送的,
默认情况下,它接受来自表单的任何内容,但是当您发送多个数据时,相应的 POST 方法需要相同的名称,因为它无法识别来自表单的数据。
例子
在这种情况下:
@using(Html.BeginForm("AddLocation", "MasterData", FormMethod.Post, new {enctype = "multipart/form-data" }))
{
<div class="file-upload">
@Html.TextBoxFor(model => model.PostedFile, new { type = "file" })
</div>
}
无论您将 POST 方法的参数命名为什么,它都会将其标识为文件
但是当用其他字段扩展 form/model 时,它缺乏检测周围文件的能力。
对于最佳实践,请按照@TetsuyaYamamoto 的建议使用强类型 ViewModel。
我已经尝试了在 Whosebug 上找到的所有解决方案,但它们对我不起作用。我也尝试过 Tetsuya Yamamoto 解决方案,但当我将它与模型一起使用时,它仍然总是返回 null
。
更新
当我检查它时,我的文件类型输入保存数据,但在检查元素中它的值为 ""
两种方式我发布的文件都是这样的
@using (Html.BeginForm("AddLocation", "MasterData", FormMethod.Post, new { encytype = "multipart/form-data"}))
{
<div class="file-upload">
<input type="file" name="postedFile" />
</div>
//passing model value when using it
}
没有模型正常工作
public ActionResult AddLocation(HttpPostedFileBase file)
{
try
{
if (file != null) //Working Perfact
{
}
return View(model);
}
catch (Exception ex)
{
return View(model);
throw;
}
}
模型总是重新运行 null
public ActionResult AddLocation(LocationModel model, HttpPostedFileBase file)
{
try
{
if (ModelState.IsValid)
{
if (file != null) //Always return null when passing with model
{
}
}
return View(model);
}
catch (Exception ex)
{
return View(model);
throw;
}
}
您可以在模型中添加 HttpPostedFileBase 文件,它应该会获取该文件。
public class LocationModel
{
....
public HttpPostedFileBase File{ get; set; }
}
原因是多个 post 参数在 asp.net mvc
中不受支持只需在视图模型 class 中添加 HttpPostedFileBase
属性 ,它与 <input type="file" />
元素具有相同的名称:
public class LocationModel
{
// other properties
public HttpPostedFileBase PostedFile { get; set; }
}
然后从控制器操作中删除第二个参数:
[HttpPost]
public ActionResult AddLocation(LocationModel model)
{
// do something
}
并用强类型绑定替换 <input>
文件元素:
@Html.TextBoxFor(model => model.PostedFile, new { type = "file" })
之后,您的文件应该在相应的视图模型上可用 属性。
旁注:
表单定义有错别字,应该使用enctype
而不是encytype
:
@model LocationModel
@using (Html.BeginForm("AddLocation", "MasterData", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="file-upload">
@Html.TextBoxFor(model => model.PostedFile, new { type = "file" })
</div>
}
参考:
mvc upload file with model - second parameter posted file is null
@ArunPratap 这个问题在 asp.net mvc 中相当有名, 实际上 POST 方法获取与视图中给定 ID 名称相同的文件, 由于表单数据是通过 headers 发送的, 默认情况下,它接受来自表单的任何内容,但是当您发送多个数据时,相应的 POST 方法需要相同的名称,因为它无法识别来自表单的数据。
例子 在这种情况下:
@using(Html.BeginForm("AddLocation", "MasterData", FormMethod.Post, new {enctype = "multipart/form-data" }))
{
<div class="file-upload">
@Html.TextBoxFor(model => model.PostedFile, new { type = "file" })
</div>
}
无论您将 POST 方法的参数命名为什么,它都会将其标识为文件 但是当用其他字段扩展 form/model 时,它缺乏检测周围文件的能力。 对于最佳实践,请按照@TetsuyaYamamoto 的建议使用强类型 ViewModel。