#眉標=MVC、ASP.NET、 #副標=ASP.NET網站開發與應用(3) #大標=ASP.NET MVC Framework建置網路商店 #作者=文/圖 王寧疆 ===<反灰>============= public void Details(int id) { } ================ ===<反灰>============= protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); } ================ ===<反灰>============= using MVCStore.Models; ================ ===<反灰>============= public ActionResult Index(string Category, int? PageIndex) { NorthwindDataContext dc = new NorthwindDataContext(); //建立類別物件 IEnumerable products = dc.Products; //取得資料表記錄 if (Category != "") //如果有查詢到產品種類條件 { products = products.Where( product => product.Category.CategoryName == Category);//取出指定種類的記錄 } return View(products.ToList()); //將查詢的結果交給View顯示 } ================ ===<反灰>============= public ActionResult Details(int id) { NorthwindDataContext dc = new NorthwindDataContext(); //建立類別物件 IEnumerable product = from p in dc.Products where p.ProductID == id select p; //取出ProductID產品記錄 return View(product.SingleOrDefault()); //查詢結果給View顯示 } ================ ===<反灰>============= [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(FormCollection collection) { try { NorthwindDataContext dc = new NorthwindDataContext(); //建立類別物件 Product p=new Product(); //建立Product物件 p.ProductName=collection[ "ProductName"]; //使用者輸入的ProductName給Product的ProductName p.SupplierID = Convert.ToInt32(collection[ "SupplierID"]); //使用者輸入的SupplierID給Product的SupplierID p.CategoryID = Convert.ToInt32(collection[ "CategoryID"]); //使用者輸入的CategoryID給Product的CategoryID p.QuantityPerUnit = collection["QuantityPerUnit"];//使用者輸入的QuantityPerUnit給Product的QuantityPerUnit p.UnitPrice = Convert.ToDecimal(collection[ "UnitPrice"]); //使用者輸入的UnitPrice給Product的UnitPrice p.UnitsInStock = Convert.ToInt16(collection[ "UnitsInStock"]); //使用者輸入的UnitsInStock給Product的UnitsInStock p.UnitsOnOrder = Convert.ToInt16(collection[ "UnitsOnOrder"]);//使用者輸入的UnitsOnOrder給Product物件的UnitsOnOrder p.ReorderLevel = Convert.ToInt16(collection[ "ReorderLevel"]);//使用者輸入的ReorderLevel給Product物件的ReorderLevel p.Discontinued = Convert.ToBoolean(collection[ "Discontinued"]);//使用者輸入的Discontinued給Product物件的Discontinued dc.Products.InsertOnSubmit(p); //準備好的Product物件加入到Products資料表 dc.SubmitChanges(); //新增的記錄寫回到資料庫 return RedirectToAction("Index");//顯示Index View } Catch//處理例外 { return View(); //顯示預設的View } } ================ ===<反灰>============= public ActionResult Edit(int id) { NorthwindDataContext dc = new NorthwindDataContext(); //建立類別物件 Product product = dc.Products.Single( p => p.ProductID == id); //取得ProductID記錄,建立成Product物件 return View(product); //將Product物件的內容顯示到View上編輯 } ================ ===<反灰>============= [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, FormCollection collection) { try { NorthwindDataContext dc = new NorthwindDataContext(); //建立類別物件 Product product = dc.Products.Single( p => p.ProductID == id); product.ProductName = collection[ "ProductName"]; //使用者輸入的ProductName給Product的ProductName product.SupplierID = Convert.ToInt32(collection[ "SupplierID"]); //使用者輸入的SupplierID給Product的SupplierID product.CategoryID = Convert.ToInt32(collection[ "CategoryID"]); //使用者輸入的CategoryID給Product的CategoryID product.QuantityPerUnit = collection[ "QuantityPerUnit"]; product.UnitPrice = Convert.ToDecimal(collection[ "UnitPrice"]); product.UnitsInStock = Convert.ToInt16(collection[ "UnitsInStock"]); product.UnitsOnOrder = Convert.ToInt16(collection[ "UnitsOnOrder"]); product.ReorderLevel = Convert.ToInt16(collection[ "ReorderLevel"]); product.Discontinued = Convert.ToBoolean(collection[ "Discontinued"]); dc.SubmitChanges();//新增的記錄寫回資料庫 return RedirectToAction("Index");//顯示Index View內容 } Catch //處理例外 { return View();//顯示預設的View內容 } } ================ ===<反灰>============= using MVCStore.Models; ================ ===<反灰>============= public class CategoriesController : Controller { public ActionResult Index() { NorthwindDataContext dc = new NorthwindDataContext(); //建立類別物件 IEnumerable categories = from c in dc.Categories select c; //取出Categories記錄 return View(categories.ToList()); //查詢所得交給View顯示 } public ActionResult Details(int id) { NorthwindDataContext dc = new NorthwindDataContext(); //建立類別物件 IEnumerable category = from c in dc.Categories where c.CategoryID == id select c; //查詢指定CategoryID記錄 return View(category.SingleOrDefault()); //查詢所得交給View顯示 } public ActionResult ShowPhoto(int id) { NorthwindDataContext dc = new NorthwindDataContext(); //建立類別物件 var picture = from c in dc.Categories where c.CategoryID==id select c.Picture; //查詢CategoryID內容 ImageResult result = new ImageResult(picture.First().ToArray(),"image/jpeg"); //將商品種類的圖案內容建立成ImageResult return result; //傳回ImageResult } } ================ ===<反灰>============= using System.Web.Mvc; using System.IO; ================ ===<反灰>============= public class ImageResult : ActionResult { public String ContentType { get; set; }//管理圖案內容類型的屬性 public byte[] ImageBytes { get; set; }//管理圖案內容的屬性 public String SourceFilename { get; set; }//管理圖檔名稱的屬性 public ImageResult(String sourceFilename, String contentType)//建構函式 { SourceFilename = sourceFilename;//給SourceFilename管理 ContentType = contentType;//給ContentType管理 } public ImageResult(byte[] sourceStream, String contentType)//建構函式 { ImageBytes = sourceStream;//給ImageBytes管理 ContentType = contentType;//給ContentType管理 } public override void ExecuteResult(ControllerContext context)//顯示圖案內容 { var response = context.HttpContext.Response;//取得HttpResponse物件 response.Clear();//呼叫HttpResponse的Clear,清除已顯示的內容 response.Cache.SetCacheability(HttpCacheability.NoCache);//關閉內容快取 response.ContentType = ContentType; //設定輸出內容類型 if (ImageBytes != null)//如果有圖案內容 { var stream=new MemoryStream(ImageBytes);//將圖案內容建立成MemoryStream stream.WriteTo(response.OutputStream);//輸出MemoryStream內容 stream.Dispose();//丟棄MemoryStream } else //如果沒有圖案內容 { response.TransmitFile(SourceFilename); //輸出圖檔的內容 } } } ================ ===<反灰>============= <%= Html.ActionLink("Edit", "Edit", new { id=item.CategoryID }) %> | ================ ===<反灰>=============

<%= Html.ActionLink("Create New", "Create") %>

================ ===<反灰>============= <%=Html.ActionLink("Edit", "Edit", new { id=Model.CategoryID }) %> | ================ ===<反灰>============= Fields   
CategoryID: <%= Html.Encode(Model.CategoryID) %>
CategoryName: <%= Html.Encode(Model.CategoryName)%>
Description: <%= Html.Encode(Model.Description)%>
Picture:
================ ===<反灰>============= ================ ===<反灰>============= public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Products",//Route name "{controller}/{action}/{Category}",//URL with parameters new { controller = "Products", action = "Index", Category = "" }//Parameter defaults ); } ================