瞧一瞧,看一看吶用MVC+EF快速弄出一個CRUD,一行代碼都不用寫,真的一行代碼都不用寫!!!!
現在要寫的呢就是,用MVC和EF弄出一個CRUD四個頁面和一個列表頁面的一個快速DEMO,當然是在不寫一行代碼的情況下.因為VS是這個做到這個的.條件呢,你需要一個VS2012或者VS2013和一個打開的數據庫.至於2010可不可以,這個我尚不可知.這篇文章呢,有點長,主要是圖,可以讓一個沒有接觸過EF的人看我的圖就可以弄一個DEMO出來了且一行代碼都不用敲,個人認為能做到不敲代碼而弄出這個的一個DEMO來倒並不是難事,點點幾下也就可以了,最主要的是能做出來然后參透其中代碼的意思,比如一些HTML的一些輔助方法,所以我這里也將其代碼都一一附上了.另外我是有寫過關於HTML輔助方法的文章的,有興趣的可以看一下我另外寫的文章.最好的是能再創建控制器的時候能創建一個空的控制器而用諸如HTML輔助方法等實現此DEMO,那樣想必是學到很多的東西了.好吧,閑話少說,咱們開始吧.
第一步,創建一個MVC4或者MVC3的一個項目,如果用13就只能創MVC4的項目了.它是會跟我們生成一個項目文件的,比如Controller和View等(此可見下面的第十幅圖),創建的圖如下:

第二步,選擇一個空的模板,引擎就選擇Razor吧(2012的話就只有三個可以選擇了),如圖:

第三步,添加一個新建項,如圖:

第四步,在新建項里,數據中添加一個ADO.NET數據實體模型,並將此模型的名字改成Info,因為我數據庫里的表名是叫Info的.如圖:

第五步,選擇從數據庫中生成.如圖:

第六步,新建一個連接,表示聯到哪個服務.不用管下圖中的那個下一步的框,是我多框了一下.如圖:

第七步,選擇要連接服務名字(我的是.\SQLEXPRESS)和哪個數據庫(我的Info表是在DPSL這個數據庫中的),並測試一下能否聯系上.如圖:

第九步,選擇一個表,模型命名空間一般會自動幫我們選擇,一般來說也不用我們改,如圖:

就這樣就可以跟我們在文件里跟我們生成一個可視的一個模型了,其與數據庫中的數據字段都是一樣的.如圖:

第十步,在Controllel中選擇添加一個控制器,如圖:

最后一步呢,就是在控制器里寫上控制器名字,此處我是將其改成了名叫Info的控制器,模板就選擇有讀寫操作的(如果不選擇有讀寫操作的就實現不了了),2013的話會有六個供你選擇,12則會有三個讓你選擇,數據上下文選擇的就是這個服務中的數據庫的名字加Entities,會有下拉框讓你選擇的.模型類就是這個表的名字,依舊會有下拉框供你選擇.這個就不要弄混了.當然在進行這一步的時候,一定要把整個項目先生成一下,否則是沒有模型和數據上下文供你選擇的.就這樣就自動會有五個頁面幫你自動生成了.如圖:

列表頁面是這樣的:

且附上其頁面的代碼:
1 @model IEnumerable<JustTest.Models.Info> 2 3 @{ 4 ViewBag.Title = "Index"; 5 } 6 7 <h2>Index</h2> 8 9 <p> 10 @Html.ActionLink("Create New", "Create") 11 </p> 12 <table> 13 <tr> 14 <th> 15 @Html.DisplayNameFor(model => model.name) 16 </th> 17 <th> 18 @Html.DisplayNameFor(model => model.age) 19 </th> 20 <th> 21 @Html.DisplayNameFor(model => model.sex) 22 </th> 23 <th></th> 24 </tr> 25 26 @foreach (var item in Model) { 27 <tr> 28 <td> 29 @Html.DisplayFor(modelItem => item.name) 30 </td> 31 <td> 32 @Html.DisplayFor(modelItem => item.age) 33 </td> 34 <td> 35 @Html.DisplayFor(modelItem => item.sex) 36 </td> 37 <td> 38 @Html.ActionLink("Edit", "Edit", new { id=item.id }) | 39 @Html.ActionLink("Details", "Details", new { id=item.id }) | 40 @Html.ActionLink("Delete", "Delete", new { id=item.id }) 41 </td> 42 </tr> 43 } 44 45 </table>
增加的頁面將會是這樣(另外附上此頁面的代碼):
1 @model JustTest.Models.Info 2 3 @{ 4 ViewBag.Title = "Create"; 5 } 6 7 <h2>Create</h2> 8 9 @using (Html.BeginForm()) { 10 @Html.AntiForgeryToken() 11 @Html.ValidationSummary(true) 12 13 <fieldset> 14 <legend>Info</legend> 15 16 <div class="editor-label"> 17 @Html.LabelFor(model => model.name) 18 </div> 19 <div class="editor-field"> 20 @Html.EditorFor(model => model.name) 21 @Html.ValidationMessageFor(model => model.name) 22 </div> 23 24 <div class="editor-label"> 25 @Html.LabelFor(model => model.age) 26 </div> 27 <div class="editor-field"> 28 @Html.EditorFor(model => model.age) 29 @Html.ValidationMessageFor(model => model.age) 30 </div> 31 32 <div class="editor-label"> 33 @Html.LabelFor(model => model.sex) 34 </div> 35 <div class="editor-field"> 36 @Html.EditorFor(model => model.sex) 37 @Html.ValidationMessageFor(model => model.sex) 38 </div> 39 40 <p> 41 <input type="submit" value="Create" /> 42 </p> 43 </fieldset> 44 } 45 46 <div> 47 @Html.ActionLink("Back to List", "Index") 48 </div> 49 50 @*@section Scripts {.. 51 @Scripts.Render("~/bundles/jqueryval")//此三行代碼為我手動注釋,如不注釋會報那個錯. 52 }*@
修改頁面將會是這樣(且附上代碼):
1 @model JustTest.Models.Info 2 3 @{ 4 ViewBag.Title = "Edit"; 5 } 6 7 <h2>Edit</h2> 8 9 @using (Html.BeginForm()) { 10 @Html.AntiForgeryToken() 11 @Html.ValidationSummary(true) 12 13 <fieldset> 14 <legend>Info</legend> 15 16 @Html.HiddenFor(model => model.id) 17 18 <div class="editor-label"> 19 @Html.LabelFor(model => model.name) 20 </div> 21 <div class="editor-field"> 22 @Html.EditorFor(model => model.name) 23 @Html.ValidationMessageFor(model => model.name) 24 </div> 25 26 <div class="editor-label"> 27 @Html.LabelFor(model => model.age) 28 </div> 29 <div class="editor-field"> 30 @Html.EditorFor(model => model.age) 31 @Html.ValidationMessageFor(model => model.age) 32 </div> 33 34 <div class="editor-label"> 35 @Html.LabelFor(model => model.sex) 36 </div> 37 <div class="editor-field"> 38 @Html.EditorFor(model => model.sex) 39 @Html.ValidationMessageFor(model => model.sex) 40 </div> 41 42 <p> 43 <input type="submit" value="Save" /> 44 </p> 45 </fieldset> 46 } 47 48 <div> 49 @Html.ActionLink("Back to List", "Index") 50 </div> 51 52 @*@section Scripts { 53 @Scripts.Render("~/bundles/jqueryval")此三行代碼為我手動注釋,如不手動注釋會報錯. 54 }*@
刪除的頁面將會是這樣(依舊附上代碼):

1 @model JustTest.Models.Info 2 3 @{ 4 ViewBag.Title = "Delete"; 5 } 6 7 <h2>Delete</h2> 8 9 <h3>Are you sure you want to delete this?</h3> 10 <fieldset> 11 <legend>Info</legend> 12 13 <div class="display-label"> 14 @Html.DisplayNameFor(model => model.name) 15 </div> 16 <div class="display-field"> 17 @Html.DisplayFor(model => model.name) 18 </div> 19 20 <div class="display-label"> 21 @Html.DisplayNameFor(model => model.age) 22 </div> 23 <div class="display-field"> 24 @Html.DisplayFor(model => model.age) 25 </div> 26 27 <div class="display-label"> 28 @Html.DisplayNameFor(model => model.sex) 29 </div> 30 <div class="display-field"> 31 @Html.DisplayFor(model => model.sex) 32 </div> 33 </fieldset> 34 @using (Html.BeginForm()) { 35 @Html.AntiForgeryToken() 36 <p> 37 <input type="submit" value="Delete" /> | 38 @Html.ActionLink("Back to List", "Index") 39 </p> 40 }
再附上詳情頁和控制器的代碼,圖就不傳了.
1 @model JustTest.Models.Info 2 3 @{ 4 ViewBag.Title = "Details"; 5 } 6 7 <h2>Details</h2> 8 9 <fieldset> 10 <legend>Info</legend> 11 12 <div class="display-label"> 13 @Html.DisplayNameFor(model => model.name) 14 </div> 15 <div class="display-field"> 16 @Html.DisplayFor(model => model.name) 17 </div> 18 19 <div class="display-label"> 20 @Html.DisplayNameFor(model => model.age) 21 </div> 22 <div class="display-field"> 23 @Html.DisplayFor(model => model.age) 24 </div> 25 26 <div class="display-label"> 27 @Html.DisplayNameFor(model => model.sex) 28 </div> 29 <div class="display-field"> 30 @Html.DisplayFor(model => model.sex) 31 </div> 32 </fieldset> 33 <p> 34 @Html.ActionLink("Edit", "Edit", new { id=Model.id }) | 35 @Html.ActionLink("Back to List", "Index") 36 </p>
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.Entity; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Mvc; 8 using MvcApplication3.Models; 9 10 namespace MvcApplication3.Controllers 11 { 12 public class InfoController : Controller 13 { 14 private DPSLEntities db = new DPSLEntities(); 15 16 // 17 // GET: /Info/ 18 19 public ViewResult Index() 20 { 21 return View(db.Info.ToList()); 22 } 23 24 // 25 // GET: /Info/Details/5 26 27 public ViewResult Details(int id) 28 { 29 Info info = db.Info.Find(id); 30 return View(info); 31 } 32 33 // 34 // GET: /Info/Create 35 36 public ActionResult Create() 37 { 38 return View(); 39 } 40 41 // 42 // POST: /Info/Create 43 44 [HttpPost] 45 public ActionResult Create(Info info) 46 { 47 if (ModelState.IsValid) 48 { 49 db.Info.Add(info); 50 db.SaveChanges(); 51 return RedirectToAction("Index"); 52 } 53 54 return View(info); 55 } 56 57 // 58 // GET: /Info/Edit/5 59 60 public ActionResult Edit(int id) 61 { 62 Info info = db.Info.Find(id); 63 return View(info); 64 } 65 66 // 67 // POST: /Info/Edit/5 68 69 [HttpPost] 70 public ActionResult Edit(Info info) 71 { 72 if (ModelState.IsValid) 73 { 74 db.Entry(info).State = EntityState.Modified; 75 db.SaveChanges(); 76 return RedirectToAction("Index"); 77 } 78 return View(info); 79 } 80 81 // 82 // GET: /Info/Delete/5 83 84 public ActionResult Delete(int id) 85 { 86 Info info = db.Info.Find(id); 87 return View(info); 88 } 89 90 // 91 // POST: /Info/Delete/5 92 93 [HttpPost, ActionName("Delete")] 94 public ActionResult DeleteConfirmed(int id) 95 { 96 Info info = db.Info.Find(id); 97 db.Info.Remove(info); 98 db.SaveChanges(); 99 return RedirectToAction("Index"); 100 } 101 102 protected override void Dispose(bool disposing) 103 { 104 db.Dispose(); 105 base.Dispose(disposing); 106 } 107 } 108 }
然后就這樣搞定了.然后最好還是能創建空控制器把這個DEMO搞定.因為這樣做在開發中想必是用不到的。而是要在這個不用寫代碼的DEMO里學到東西。另外我這邊用13創建的時候出了點小問題,在創建鏈接和增加鏈接的時候會報一個錯.是說,"當前上下文中不存在名稱"Scripts".然后我將這兩個頁面最后的代碼刪除了,也就沒有這個錯了.如果有大神看到了這里且知道原因的話,煩請告之.共同進步.
