zoukankan      html  css  js  c++  java
  • MVC第一个增删查改

    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();
                }
            }
        }
    }
  • 相关阅读:
    读取XML示例:C#获取XML的数据
    GridView不換行
    List<T> 的条件筛选 where使用方法
    [HDU] 1016 Prime Ring Problem
    [HDU] 1072 Nightmare 和HDU1180有点类似
    [HDU] 1180 诡异的楼梯个人觉得比较有趣的广搜索
    [HDU] 1312Red and Black 用广搜求能探寻到的点的数目
    [HDU] 1026 Ignatius and the Princess I 简单建模后广搜索求最短路径生成树
    [HDU] 1010 Tempter of the Bone最基本的深搜
    [HDU] 1175 连连看 剪枝优化后的性能飙升
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050633.html
Copyright © 2011-2022 走看看