Usuário com melhor resposta
MVC3 com ASP.NET Visual Studio 2012

Pergunta
-
estou tentando desenvolver um aplicativo ASP.NET com Sql Server. Consegui gravar dessa forma :
tabela.Cliente.Add(GravaCliente);
tabela.SaveChanges();
return RedirectToAction("Index");O modo com eu fazia com o Visual Studio 2010 não funciona mais.
Gostaria que alguém me ajudasse com o Update e o Delete dento do (controller). Agradeço a gentileza.
Respostas
-
public ActionResult Edit(int id, FormCollection collection) { if(! ModelState.IsValid) return View(); try { var selCliente = (from cat in tabela.Cliente where cat.Id == id select cat).First(); tabela.Entry(selCliente).State = System.Data.EntityState.Deleted; tabela.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }
http://www.linkedin.com/pub/murilo-kunze/44/191/455
- Marcado como Resposta hhudsonpc segunda-feira, 17 de dezembro de 2012 18:30
Todas as Respostas
-
tabela.Entry(objeto).State = EntityState.Modified; tabela.Entry(objeto).State = EntityState.Deleted;
ps: EntityState pertence a System.Data.
http://www.linkedin.com/pub/murilo-kunze/44/191/455
- Editado Murilo Kunze segunda-feira, 17 de dezembro de 2012 14:44
-
-
-
-
-
Olá Murilo, estou te dando um bocado de trabalho, o objeto não é reconhecido. Seguinte:
Consegui editar assim:
public ActionResult Edit(int id, FormCollection collection)
{
try
{
var selCliente = (from cat in tabela.Cliente where cat.Id == id select cat).First();
if(! ModelState.IsValid)
return View();
UpdateModel(selCliente);
tabela.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}ainda falta o delete.
-
public ActionResult Edit(int id, FormCollection collection) { if(! ModelState.IsValid) return View(); try { var selCliente = (from cat in tabela.Cliente where cat.Id == id select cat).First(); tabela.Entry(selCliente).State = System.Data.EntityState.Deleted; tabela.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }
http://www.linkedin.com/pub/murilo-kunze/44/191/455
- Marcado como Resposta hhudsonpc segunda-feira, 17 de dezembro de 2012 18:30
-