MVC入门

public class UserInfoQuickController : Controller
    {
        //
        // GET: /UserInfoQuick/

        Hemam2DbEntities db = new Hemam2DbEntities();

        public ActionResult Index()
        {
            ViewData.Model = db.UserInfo;

            return View();
        }

        //
        // GET: /UserInfoQuick/Details/5

        public ActionResult Details(int id)
        {
            return View(db.UserInfo.Where<UserInfo>(u => u.ID == id).FirstOrDefault());
        }

        //
        // GET: /UserInfoQuick/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /UserInfoQuick/Create

        [HttpPost]
        public ActionResult Create(UserInfo userInfo)
        {
            try
            {
                // TODO: Add insert logic here
                db.UserInfo.AddObject(userInfo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(userInfo);
            }
        }

        //
        // GET: /UserInfoQuick/Edit/5

        public ActionResult Edit(int id)
        {
            //

            return View(db.UserInfo.Where<UserInfo>(u => u.ID == id).FirstOrDefault());
        }

        //
        // POST: /UserInfoQuick/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, UserInfo userInfo)
        {
            try
            {
                // TODO: Add update logic here
                db.UserInfo.Attach(userInfo);
                db.ObjectStateManager.ChangeObjectState(userInfo, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /UserInfoQuick/Delete/5

        public ActionResult Delete(int id)
        {
            return View(db.UserInfo.Where<UserInfo>(u => u.ID == id).FirstOrDefault());
        }

        //
        // POST: /UserInfoQuick/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, UserInfo userInfo)
        {
            try
            {
                // TODO: Add delete logic here
                userInfo.ID = id;

                db.UserInfo.Attach(userInfo);
                db.ObjectStateManager.ChangeObjectState(userInfo, EntityState.Deleted);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
posted on 2013-01-23 19:04  快乐于行  阅读(247)  评论(0编辑  收藏  举报