This is my insert update code
At the time of inserting insert data successfully but at the time of Update not updateing the data Please See my code below
[HttpPost]
public ActionResult Create(HttpPostedFileBase imageFile, Actor model, string cmd)
{
try
{
if (imageFile != null && imageFile.ContentLength > 0)
{
var fileName = Path.GetFileName(imageFile.FileName);
if (cmd == "Create")
{
model.Image = fileName;
db.Actors.Add(model);
db.SaveChanges();
}
else
{
var actor = db.Actors.Where(m => m.ActorID == model.ActorID).FirstOrDefault();
if (actor != null)
{
actor.Image = fileName;
db.SaveChanges();
}
}
var path = Path.Combine(Server.MapPath("~/Content/Actors/"), model.ActorID.ToString());
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
imageFile.SaveAs(path + "/" + fileName);
}
}
catch { }
return RedirectToAction("Index");
}
UPDATE:
[HttpGet]
public ActionResult Edit(int id)
{
var actor = db.Actors.Where(m => m.ActorID == id).FirstOrDefault();
ViewBag.IsUpdate = true;
return PartialView("_Actor", actor);
}
[HttpPost]
public ActionResult Edit(HttpPostedFileBase imageFile, Actor model, string cmd)
{
if(ModelState.IsValid)
{
try
{
if (imageFile != null && imageFile.ContentLength > 0)
{
var fileName = Path.GetFileName(imageFile.FileName);
if (cmd == "Update")
{
model.Image = fileName;
db.Actors.Add(model);
db.SaveChanges();
}
else
{
var actor = db.Actors.Where(m => m.ActorID == model.ActorID).FirstOrDefault();
if (actor != null)
{
actor.Image = fileName;
db.SaveChanges();
}
}
var path = Path.Combine(Server.MapPath("~/Content/Actors/"), model.ActorID.ToString());
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
imageFile.SaveAs(path + "/" + fileName);
}
}
}
catch { }
return RedirectToAction("Index");
}