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();
                }
            }
        }
    }
  • 相关阅读:
    使用bash编写Linux shell脚本参数和子壳
    开发项目的简单流程(需求、数据库、编码)
    hadoop和Hive的数据处理流程
    数据分析
    模糊聚类分析的实现
    贝叶斯1
    代理猎手
    贝叶斯2
    模糊聚类算法(FCM)和硬聚类算法(HCM)的VB6.0实现及
    C++模板
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050633.html
Copyright © 2011-2022 走看看