【发布时间】:2016-03-04 17:19:20
【问题描述】:
我需要上传一张图片并将其保存到一个文件中,但我无法在控制器中捕获它,因为它始终为空。
在视图中:
@using(Html.BeginForm("Create", "Products", FormMethod.Post, new { enctype = "multipart/form-data "}))
{
....
<input type="file" name="file" id="file" />
控制器:
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ProductID,FKSubCategoryID,ProductEnName,ProductArName,ProductEnDescription,ProductArDescription,ProductImage")] Product product,HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
product.FKBrandID = 1;
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
我已经试过了:
- 使用 HttpPostedFileBase 代替 HttpPostedFile
-
Request.Files["file"]是 null - 输入中的参数名称与ActionResult相同
enctype = "multipart/form-data ", data_ajax = "false"- 添加
[AcceptVerbs(HttpVerbs.Post)]
这让我发疯了,请大家给点建议?
【问题讨论】:
-
你如何提交这个(标准提交或 ajax)?
-
使用标准提交
-
那么
data_ajax = "false"是什么?哪个建议ajax? - 如果你有@usng (Html.BeginForm(......, new { enctype = "multipart/form-data" }))它应该可以正常工作 -
我尝试了使用和不使用 data_ajax = "false" 以防万一。我有 (Html.BeginForm("Create", "Products", FormMethod.Post, new { enctype = " multipart/form-data "})) 仍然没有文件
-
您在
name="file"的视图中还有其他输入吗?