Update data in to database using MVC

Posted by Mandlaa under Others on 7/4/2013 | Points: 10 | Views : 1241 | Status : [Member] | Replies : 0
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");
}




Responses

(No response found.)

Login to post response