博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET MVC 5 自动生成的代码框架
阅读量:4701 次
发布时间:2019-06-09

本文共 5088 字,大约阅读时间需要 16 分钟。

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/5102         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/5117         [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 }

 

转载于:https://www.cnblogs.com/wanghaibin/articles/5522942.html

你可能感兴趣的文章
jquery json 结合
查看>>
压缩OLEVARIANT数据
查看>>
ZOJ 3687
查看>>
HIHO 16 B
查看>>
wordpress 开源博客系统部署
查看>>
MySQL Community Server 5.6和MySQL Installer 5.6
查看>>
hdu5282 最长公共子序列的变形
查看>>
SQL分页查询总结{转}
查看>>
迭代加深搜索 POJ 1129 Channel Allocation
查看>>
.Net调用Java带验证的WebService解决方法
查看>>
手机web下滑加载
查看>>
LeetCode 20. 有效的括号
查看>>
TCP 接收窗口自动调节
查看>>
JavaScript Patterns 3.7 Primitive Wrappers
查看>>
一个打砖块的小游戏1.0 KILL THE BLOCKS !
查看>>
学习、发现和创造-一切皆有规律
查看>>
107.为什么要使用数据库之文件增删查改直接操作
查看>>
深度学习笔记 (一) 卷积神经网络基础 (Foundation of Convolutional Neural Networks)
查看>>
python三个自带装饰器的功能与使用(@property、@staticmethod、@classmethod)
查看>>
三分法——求解凸性函数的极值问题
查看>>