ASP.NET MVC 5 自动生成的代码框架

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Data.Entity;
  5 using System.Linq;
  6 using System.Net;
  7 using System.Web;
  8 using System.Web.Mvc;
  9 using LinqDemo3.Models;
 10 
 11 namespace LinqDemo3.Controllers
 12 {
 13     public class ProductsController : Controller
 14     {
 15         private Model1 db = new Model1();
 16 
 17         // GET: Products
 18         public ActionResult Index()
 19         {
 20             var products = db.Products.Include(p => p.Category).Include(p => p.Supplier);
 21             return View(products.ToList());
 22         }
 23 
 24         // GET: Products/Details/5
 25         public ActionResult Details(int? id)
 26         {
 27             if (id == null)
 28             {
 29                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
 30             }
 31             Product product = db.Products.Find(id);
 32             if (product == null)
 33             {
 34                 return HttpNotFound();
 35             }
 36             return View(product);
 37         }
 38 
 39         // GET: Products/Create
 40         public ActionResult Create()
 41         {
 42             ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName");
 43             ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName");
 44             return View();
 45         }
 46 
 47         // POST: Products/Create
 48         // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关 
 49         // 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598
 50         [HttpPost]
 51         [ValidateAntiForgeryToken]
 52         public ActionResult Create([Bind(Include = "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Product product)
 53         {
 54             if (ModelState.IsValid)
 55             {
 56                 db.Products.Add(product);
 57                 db.SaveChanges();
 58                 return RedirectToAction("Index");
 59             }
 60 
 61             ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID);
 62             ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName", product.SupplierID);
 63             return View(product);
 64         }
 65 
 66         // GET: Products/Edit/5
 67         public ActionResult Edit(int? id)
 68         {
 69             if (id == null)
 70             {
 71                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
 72             }
 73             Product product = db.Products.Find(id);
 74             if (product == null)
 75             {
 76                 return HttpNotFound();
 77             }
 78             ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID);
 79             ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName", product.SupplierID);
 80             return View(product);
 81         }
 82 
 83         // POST: Products/Edit/5
 84         // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关 
 85         // 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598
 86         [HttpPost]
 87         [ValidateAntiForgeryToken]
 88         public ActionResult Edit([Bind(Include = "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Product product)
 89         {
 90             if (ModelState.IsValid)
 91             {
 92                 db.Entry(product).State = EntityState.Modified;
 93                 db.SaveChanges();
 94                 return RedirectToAction("Index");
 95             }
 96             ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID);
 97             ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName", product.SupplierID);
 98             return View(product);
 99         }
100 
101         // GET: Products/Delete/5
102         public ActionResult Delete(int? id)
103         {
104             if (id == null)
105             {
106                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
107             }
108             Product product = db.Products.Find(id);
109             if (product == null)
110             {
111                 return HttpNotFound();
112             }
113             return View(product);
114         }
115 
116         // POST: Products/Delete/5
117         [HttpPost, ActionName("Delete")]
118         [ValidateAntiForgeryToken]
119         public ActionResult DeleteConfirmed(int id)
120         {
121             Product product = db.Products.Find(id);
122             db.Products.Remove(product);
123             db.SaveChanges();
124             return RedirectToAction("Index");
125         }
126 
127         protected override void Dispose(bool disposing)
128         {
129             if (disposing)
130             {
131                 db.Dispose();
132             }
133             base.Dispose(disposing);
134         }
135     }
136 }

 

posted @ 2016-05-24 11:57  海阔天空XM  阅读(838)  评论(0)    收藏  举报