This is my upload Action :
[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();
}
And this is my view :
<% using (Html.BeginForm("addcar", "Agence", FormMethod.Post, new { @class = "search_form", @enctype = "multipart/form-data" }))
I have used this same function already in other code it works fine here i dont know whi it dosen’t work, The reste of action works fine but the photo is not uploaded
What does your View look like? Are you including the atribute enctype = “multipart/form-data” on your form tag in the view?
Verify @ sign, not needed on enctype