Stephen Walther 從頭開始到結(jié)束建立了整個數(shù)據(jù)驅(qū)動ASP.NET MVC應(yīng)用程序。這個教程對于那些剛剛接觸ASP.NET MVC框架以及想要獲得一種建立ASP.NET MVC應(yīng)用程序的過程的新人是一個很好的介紹。
為簡化創(chuàng)建應(yīng)用程序的過程,我們會充分利用Visual Studio 2008的功能。我們會讓 Visual Studio為我們的控制層,模型層,視圖層生成初始代碼和內(nèi)容。 如果你已經(jīng)使用Active Server Pages 或者 ASP.NET來工作,那么你將對ASP.NET MVC很熟悉。ASP.NET MVC 視圖與ASP應(yīng)用程序的頁面非常相似。并且與傳統(tǒng)的ASP.NET Web Forms 應(yīng)用程序一樣, ASP.NET MVC 為你提供了豐富的語言和.NET框架類。我希望這篇教程將讓你了解到創(chuàng)建ASP.NET MVC應(yīng)用程序的經(jīng)驗與創(chuàng)建ASP or ASP.NET Web Forms 應(yīng)用程序的相似點(diǎn)和不同點(diǎn).
這部電影數(shù)據(jù)庫應(yīng)用程序的概述
篇前部分(準(zhǔn)備工作)
創(chuàng)建一個ASP.NET MVC Web Application 項目
當(dāng)你創(chuàng)建一個新的MVC Web Application項目時 Visual Studio提示你去創(chuàng)造一個獨(dú)立的單元測試項目,如圖2所示。因為在這一課中我們不會創(chuàng)建測試,選擇“否”選項,并點(diǎn)擊“確定”按鈕。
ASP.NET MVC應(yīng)用程序有一系列標(biāo)準(zhǔn)的文件夾:模型、視圖和控制的文件夾。你可以在“解決方案資源管理器”中看到這一系列標(biāo)準(zhǔn)的文件夾。我們需要添加文件到模型層,視圖層和控制層文件夾,用以創(chuàng)建我們的電影的數(shù)據(jù)庫應(yīng)用程序。
接下來,我們要創(chuàng)造一個新數(shù)據(jù)庫表。從服務(wù)器資源管理器窗口,右鍵點(diǎn)擊該表格的文件夾“表”,并選擇菜單選項“添加新表”。選擇這個菜單選項打開數(shù)據(jù)庫表設(shè)計器。創(chuàng)建以下數(shù)據(jù)庫的欄目。
第一列,Id列,有兩個特殊的屬性。首先,你需要標(biāo)記Id列為主鍵列。選擇后,點(diǎn)擊Id列的“設(shè)置主鍵”選項(它是圖標(biāo)看起來像一個鑰匙)。其次,你需要標(biāo)記Id列作為一個標(biāo)記列。在這個“列屬性”窗口,向下轉(zhuǎn)動滾輪至“標(biāo)識規(guī)范”并且展開該節(jié)點(diǎn),改變“(是標(biāo)識)”為“是”。當(dāng)你完成,表應(yīng)該看起來像下圖所示。
最后一步是保存這個新表。點(diǎn)擊“保存按鈕,并且給新表命名為Movies。
創(chuàng)建模型層 你點(diǎn)擊“添加”按鈕后,這個實(shí)體數(shù)據(jù)模型向?qū)С霈F(xiàn)(見圖)。遵循這些步驟來完成向?qū)А?/p> 1。在選擇模型內(nèi)容這一步,選擇“從數(shù)據(jù)庫生成”的選擇。 圖8
在你完成實(shí)體數(shù)據(jù)模型向?qū)?,?shí)體數(shù)據(jù)模型設(shè)計器將開啟。設(shè)計器應(yīng)該顯示 Movies數(shù)據(jù)庫表(見圖10)。
在我們繼續(xù)之前,我們需要做一點(diǎn)改變。這個實(shí)體數(shù)據(jù)向?qū)梢粋€Movies的模型類,代表 Movies數(shù)據(jù)庫表。因為我們將使用這個 Movies 類代表某一特定的電影,我們需要修改這個類的名稱為Movie而不是Movies(單數(shù)而不是復(fù)數(shù))。
創(chuàng)建ASP.NET MVC 控制層
3。點(diǎn)擊“添加”按鈕來添加新的控制器。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace MovieApp.Controllers 8 { 9 public class HomeController : Controller 10 { 11 // 12 // GET: /Home/ 13 14 public ActionResult Index() 15 { 16 return View(); 17 } 18 19 // 20 // GET: /Home/Details/5 21 22 public ActionResult Details(int id) 23 { 24 return View(); 25 } 26 27 // 28 // GET: /Home/Create 29 30 public ActionResult Create() 31 { 32 return View(); 33 } 34 35 // 36 // POST: /Home/Create 37 38 [HttpPost] 39 public ActionResult Create(FormCollection collection) 40 { 41 try 42 { 43 // TODO: Add insert logic here 44 45 return RedirectToAction('Index'); 46 } 47 catch 48 { 49 return View(); 50 } 51 } 52 53 // 54 // GET: /Home/Edit/5 55 56 public ActionResult Edit(int id) 57 { 58 return View(); 59 } 60 61 // 62 // POST: /Home/Edit/5 63 64 [HttpPost] 65 public ActionResult Edit(int id, FormCollection collection) 66 { 67 try 68 { 69 // TODO: Add update logic here 70 71 return RedirectToAction('Index'); 72 } 73 catch 74 { 75 return View(); 76 } 77 } 78 79 // 80 // GET: /Home/Delete/5 81 82 public ActionResult Delete(int id) 83 { 84 return View(); 85 } 86 87 // 88 // POST: /Home/Delete/5 89 90 [HttpPost] 91 public ActionResult Delete(int id, FormCollection collection) 92 { 93 try 94 { 95 // TODO: Add delete logic here 96 97 return RedirectToAction('Index'); 98 } 99 catch 100 { 101 return View(); 102 } 103 } 104 } 105 } 106 107
列出數(shù)據(jù)庫記錄 我們將使用 Index()方法來顯示Movies數(shù)據(jù)庫表的一些數(shù)據(jù)。我們將使用Index()方法,利用數(shù)據(jù)庫模型類來檢索Movies數(shù)據(jù)庫記錄。 1 public class HomeController : Controller 2 { 3 4 // 5 // GET: /Home/ 6 7 private MovieApp.Models.MoviesDBEntities _db = new 8 9 Models.MoviesDBEntities(); 10 public ActionResult Index() 11 { 12 //return View(); 13 return View(_db.Movies.ToList()); 14 } 15 .... 16 }
Index()方法將返回一個命名為Index的視圖view。我們需要創(chuàng)建這個view來顯示movies數(shù)據(jù)庫記錄的列表。遵循這些步驟: 2。在對話框的角度,勾選“創(chuàng)建強(qiáng)類型視圖”復(fù)選框?!?br> 3。從“視圖數(shù)據(jù)類”下拉列表中,選擇值MovieApp.Models.Movie。 Listing 3 – Views\Home\Index.aspx
1 <%@ Page Title='' Language='C#' 2 3 MasterPageFile='~/Views/Shared/Site.Master' 4 5 Inherits='System.Web.Mvc.ViewPage 6 7 %> 8 9 asp:Content ID='Content1' ContentPlaceHolderID='TitleContent' 10 11 runat='server'> 12 Index 13 asp:Content> 14 15 asp:Content ID='Content2' ContentPlaceHolderID='MainContent' 16 17 runat='server'> 18 19 h2>Indexh2> 20 21 table> 22 tr> 23 th>th> 24 th> 25 Id 26 th> 27 th> 28 Title 29 th> 30 th> 31 Director 32 th> 33 th> 34 DateReleased 35 th> 36 tr> 37 38 <% foreach (var item in Model) { %> 39 40 tr> 41 td> 42 <%: Html.ActionLink('Edit', 'Edit', new { id=item.Id }) 43 44 %> | 45 <%: Html.ActionLink('Details', 'Details', new { 46 47 id=item.Id })%> | 48 <%: Html.ActionLink('Delete', 'Delete', new { 49 50 id=item.Id })%> 51 td> 52 td> 53 <%: item.Id %> 54 td> 55 td> 56 <%: item.Title %> 57 td> 58 td> 59 <%: item.Director %> 60 td> 61 td> 62 <%: String.Format('{0:g}', item.DateReleased) %> 63 td> 64 tr> 65 66 <% } %> 67 68 table> 69 70 p> 71 <%: Html.ActionLink('Create New', 'Create') %> 72 p> 73 74 asp:Content> 75 76
這個Index view 將movie數(shù)據(jù)庫表所有記錄顯示在HTML表格。這個視圖包含一個foreach循環(huán),它遍歷ViewData.Model屬性所表示的每一個movie。如果你通過按F5鍵運(yùn)行應(yīng)用程序,你將會看到網(wǎng)頁如下圖所示。 ![]() 圖14
創(chuàng)建新的數(shù)據(jù)庫記錄 我們在前一部分創(chuàng)建 的Index view 包括一個為創(chuàng)建新數(shù)據(jù)庫記錄的鏈接。
1 // 2 // POST: /Home/Create 3 [AcceptVerbs(HttpVerbs.Post)] 4 5 public ActionResult Create([Bind(Exclude = 'Id')] Movie movieToCreate) 6 { 7 8 if (!ModelState.IsValid) 9 10 return View(); 11 12 _db.AddToMovies(movieToCreate); 13 _db.SaveChanges(); 14 return RedirectToAction('Index'); 15 16 } 17
Visual Studio ,使它容易創(chuàng)建表格來創(chuàng)建一個新的movie數(shù)據(jù)庫記錄。遵循這些步驟: 1。在代碼編輯窗口,右鍵單擊Create()方法,選擇菜單選項“添加視圖”。 圖
Listing 5 – Views\Home\Create.aspx
1 <%@ Page Title='' Language='C#' MasterPageFile='~/Views/Shared/Site.Master' Inherits='System.Web.Mvc.ViewPage 2 3 asp:Content ID='Content1' ContentPlaceHolderID='TitleContent' runat='server'> 4 Create 5 asp:Content> 6 7 asp:Content ID='Content2' ContentPlaceHolderID='MainContent' runat='server'> 8 9 h2>Createh2> 10 11 <% using (Html.BeginForm()) {%> 12 <%: Html.ValidationSummary(true) %> 13 14 fieldset> 15 legend>Fieldslegend> 16 17 div class='editor-label'> 18 <%: Html.LabelFor(model => model.Id) %> 19 div> 20 div class='editor-field'> 21 <%: Html.TextBoxFor(model => model.Id) %> 22 <%: Html.ValidationMessageFor(model => model.Id) %> 23 div> 24 25 div class='editor-label'> 26 <%: Html.LabelFor(model => model.Title) %> 27 div> 28 div class='editor-field'> 29 <%: Html.TextBoxFor(model => model.Title) %> 30 <%: Html.ValidationMessageFor(model => model.Title) %> 31 div> 32 33 div class='editor-label'> 34 <%: Html.LabelFor(model => model.Director) %> 35 div> 36 div class='editor-field'> 37 <%: Html.TextBoxFor(model => model.Director) %> 38 <%: Html.ValidationMessageFor(model => model.Director) %> 39 div> 40 41 div class='editor-label'> 42 <%: Html.LabelFor(model => model.DateReleased) %> 43 div> 44 div class='editor-field'> 45 <%: Html.TextBoxFor(model => model.DateReleased) %> 46 <%: Html.ValidationMessageFor(model => model.DateReleased) %> 47 div> 48 49 p> 50 input type='submit' value='Create' /> 51 p> 52 fieldset> 53 54 <% } %> 55 56 div> 57 <%: Html.ActionLink('Back to List', 'Index') %> 58 div> 59 60 asp:Content> 61 62
在你創(chuàng)建 Create 視圖后,你可以添加新的Movie 記錄到數(shù)據(jù)庫。按下F5鍵運(yùn)行你的應(yīng)用程序和點(diǎn)擊Create New 的鏈接,你將看到如圖13所示的表格。如果你完成并提交表單、一個新movie數(shù)據(jù)庫記錄誕生了。 ![]() 注意,你自動獲得表單驗證。如果你忽略輸入movie的發(fā)行日期,或者你輸入一個無效的發(fā)布日期,那么這個表單被重新顯示,并且發(fā)布日期字段將高亮顯示。 在之前的部分,我們討論了如何列舉和創(chuàng)造新的數(shù)據(jù)庫記錄。在這最后一節(jié)中,我們將討論如何修改現(xiàn)有的數(shù)據(jù)庫記錄。 完成這些步驟,將添加一個Edit.aspx的視圖到Views\Home文件夾中。這一視圖包含用來編輯一個movie記錄的HTML表單。Edit view包含一個相應(yīng)于movie Id屬性的HTML表單字段。因為你不想讓人們編輯這個Id屬性的值,你應(yīng)該移除這個表單字段。 Listing 6 – Controllers\HomeController.cs (Edit methods) 1 // 2 // GET: /Home/Edit/5 3 4 public ActionResult Edit(int id) 5 { 6 //return View(); 7 var movieToEdit = (from m in _db.Movies 8 9 where m.Id == id 10 11 select m).First(); 12 13 return View(movieToEdit); 14 15 16 } 17 18 19 [AcceptVerbs(HttpVerbs.Post)] 20 21 public ActionResult Edit(Movie movieToEdit) 22 { 23 24 var originalMovie = (from m in _db.Movies 25 26 where m.Id == movieToEdit.Id 27 28 select m).First(); 29 30 if (!ModelState.IsValid) 31 32 return View(originalMovie); 33 34 _db.ApplyCurrentValuesMovie>(originalMovie.EntityKey.EntitySetName, movieToEdit); 35 //_db.ApplyPropertyChanges(originalMovie.EntityKey.EntitySetName, movieToEdit); 36 37 _db.SaveChanges(); 38 39 return RedirectToAction('Index'); 40 41 } 42 43
在 Listing 6,我已經(jīng)為2個重載的Edit()方法增添了更多的邏輯。第一個Edit()方法將返回相對應(yīng)的Id傳遞參數(shù)的方法的movie數(shù)據(jù)庫記錄。第二個重載方法執(zhí)行數(shù)據(jù)庫中記錄的更新。 ![]()
注意,你必須檢索到原來的movie,然后調(diào)用ApplyCurrentValues |
|