1
这是我上传的行动:图片上传故障
[HttpPost]
public ActionResult addcar(Models.vehicule model)
{
ViewBag.cat = new SelectList(entity.categorie, "Idcat", "Nom");
ViewBag.mark = new SelectList(entity.marque, "id", "nom");
if (ModelState.IsValid)
{
// Upload Function begin
var destinationFolder = Server.MapPath("/Content/Vpic");
foreach (string name in Request.Files)
{
HttpPostedFileBase postedFile = Request.Files[name];
if (postedFile.ContentLength > 0)
{
var fileName = model.Matv;
var path = Path.Combine(destinationFolder, fileName + ".jpg");
postedFile.SaveAs(path);
}
}
// upload function end
model.Idag = User.Identity.Name.ToString();
entity.vehicule.AddObject(model);
entity.SaveChanges();
TempData["Resultat"] = "L'ajout de véhicule a reussi";
return RedirectToAction("GesV", "Agence");
}
else
return View();
}
这是我的看法:
<% using (Html.BeginForm("addcar", "Agence", FormMethod.Post, new { @class = "search_form", @enctype = "multipart/form-data" }))
我已经在其他的代码已经使用的相同功能,它在这里工作细,我不知道WHI它没有工作,行动reste工作正常,但照片不上传
你的视图是什么样的?你是否在视图的form标签中包含了属性enctype =“multipart/form-data”? –
是的,我包括enctype – Chlebta
那么唯一不同的是我看到的是我设置了一个显式的参数在我的动作文件以及模型:public ActionResult addcar(Models.vehicule模型,HttpPostedFileBase文件)...否则我没有看到任何区别 –