namespace MvcApplication1.Controllers

{

[HandleError]
public
class HomeController : Controller

{

NorthwindEntities northWind =
new NorthwindEntities();
public ActionResult Index()

{

var models = northWind.Categories.ToList();
return View(models);

}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Edit(
int id)

{

var models = northWind.Categories.First(c => c.CategoryID == id);
return View(models);

}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(
int categoryId, FormCollection form)

{

var models = northWind.Categories.First(c => c.CategoryID == categoryId);

UpdateModel(models,
new[] {
"CategoryName",
"Description" });

northWind.SaveChanges();
return RedirectToAction(
"Index");

}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Detail(
int id)

{

var models = northWind.Categories.First(c => c.CategoryID == id);
return View(models);

}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Create()

{

Categories category =
new Categories();
return View(category);

}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(
int categoryId, FormCollection form)

{

var models = northWind.Categories.FirstOrDefault(c => c.CategoryID == categoryId);
if (models==
null)

{

Categories category =
new Categories();

UpdateModel(category,
new[] {
"CategoryName",
"Description" });

northWind.AddToCategories(category);

northWind.SaveChanges();
return RedirectToAction(
"Index");
}
else

{
return RedirectToAction(
"Create");

}

}
public ActionResult About()

{
return View();

}

}

}