Tuesday, January 31, 2012

Asp.net MVC and Entity Framework CRUD

public class MyController : Controller
{
//
// GET: /My/
MyDBEntities db = new MyDBEntities();
public ActionResult Index()
{
return View(db.Information.ToList());
}

//
// GET: /My/Details/5

public ActionResult Details(int id)
{
return View();
}

//
// GET: /My/Create

public ActionResult Create()
{

return View();
}

//
// POST: /My/Create

[HttpPost]
public ActionResult Create([Bind(Exclude = "Id")]Information info)
{
try
{
// TODO: Add insert logic here

db.AddToInformation(info);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}

//
// GET: /My/Edit/5

public ActionResult Edit(int id)
{


// var cc = db.Information.Select(x => x.Id == id).FirstOrDefault();

var cc = (from info in db.Information where info.Id == id select info).FirstOrDefault();


return View(cc);
}

//
// POST: /My/Edit/5

[HttpPost]
public ActionResult Edit( Information info)
{
try
{
// TODO: Add update logic here

db.Information.AddObject(info);
db.ObjectStateManager.ChangeObjectState(info, System.Data.EntityState.Modified);
// db.AcceptAllChanges();
db.SaveChanges();
return RedirectToAction("Index");
}
catch(Exception ex)
{
return View();
}
}

//
// GET: /My/Delete/5

public ActionResult Delete(int id)
{
var cc = (from info in db.Information where info.Id == id select info).FirstOrDefault();
return View(cc);
}

//
// POST: /My/Delete/5

[HttpPost]
public ActionResult Delete(Information info)
{
try
{
db.Information.AddObject(info);
db.ObjectStateManager.ChangeObjectState(info, System.Data.EntityState.Deleted);
// db.AcceptAllChanges();
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}

No comments:

Post a Comment

What is DaemonSet in Kubernetes

 A DaemonSet is a type of controller object that ensures that a specific pod runs on each node in the cluster. DaemonSets are useful for dep...