using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC实战.Models;
namespace MVC实战.Controllers
{
public class ComputerController : Controller
{
//
// GET: /Computer/
public ActionResult Index()
{
HouseContainer houseContainer = new HouseContainer();
ViewData.Model=houseContainer.Computer.AsEnumerable<Computer>();
return View();
}
//
// GET: /Computer/Details/5
public ActionResult Details(int id)
{
HouseContainer houseContainer = new HouseContainer();
var result=from d in houseContainer.Computer where d.ID==id select d;
return View(result.SingleOrDefault<Computer>());
}
//
// GET: /Computer/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Computer/Create
[HttpPost]
public ActionResult Create(Computer computer)
{
try
{
HouseContainer houseContainer = new HouseContainer();
houseContainer.Computer.AddObject(computer);
houseContainer.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Computer/Edit/5
public ActionResult Edit(int id)
{
HouseContainer houseContainer = new HouseContainer();
var result = from d in houseContainer.Computer where d.ID == id select d;
return View(result.SingleOrDefault<Computer>());
}
//
// POST: /Computer/Edit/5
[HttpPost]
public ActionResult Edit(int id, Computer computer)
{
try
{
HouseContainer houseContainer = new HouseContainer();
var oldModel = (from d in houseContainer.Computer where d.ID == id select d).SingleOrDefault<Computer>();
oldModel.CPU =computer.CPU;
oldModel.Mouse =computer.Mouse;
houseContainer.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Computer/Delete/5
public ActionResult Delete(int id)
{
HouseContainer houseContainer = new HouseContainer();
var result = from d in houseContainer.Computer where d.ID == id select d;
ViewData.Model = result.SingleOrDefault<Computer>();
return View();
}
//
// POST: /Computer/Delete/5
[HttpPost]
public ActionResult Delete(int id,Computer computer)
{
try
{
HouseContainer houseContainer = new HouseContainer();
var result = (from d in houseContainer.Computer where d.ID == id select d).SingleOrDefault<Computer>();
houseContainer.Computer.DeleteObject(result);
houseContainer.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}