- 利用ado.net添加數據庫實體類,而且生成user實體類。
- 添加Controller類。
- 添加View視圖。
實體數據添加不作說明,user代碼以下:javascript
1 //------------------------------------------------------------------------------ 2 // <auto-generated> 3 // 此代碼已從模板生成。 4 // 5 // 手動更改此文件可能致使應用程序出現意外的行爲。 6 // 若是從新生成代碼,將覆蓋對此文件的手動更改。 7 // </auto-generated> 8 //------------------------------------------------------------------------------ 9 10 namespace MVC5Test.Models 11 { 12 using System; 13 using System.Collections.Generic; 14 15 public partial class user 16 { 17 public int id { get; set; } 18 public string name { get; set; } 19 public string sex { get; set; } 20 public Nullable<int> age { get; set; } 21 public string tel { get; set; } 22 } 23 }
View文件夾下創建Test目錄,建立Create.cshtml文件,代碼以下:html
1 @model MVC5Test.Models.user 2 3 <script src="~/Scripts/jquery-1.10.2.min.js"></script> 4 <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> 5 <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> 6 <script type="text/javascript"> 7 function success(data) { 8 alert(data); 9 } 10 </script> 11 12 @using ( Ajax.BeginForm( "InsertData",new AjaxOptions { HttpMethod="Post", InsertionMode=InsertionMode.Replace, UpdateTargetId= "detailsID" })) 13 { 14 @Html.AntiForgeryToken() 15 16 <div class="form-horizontal"> 17 <h4>user</h4> 18 <hr /> 19 @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 20 <div class="form-group"> 21 @Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" }) 22 <div class="col-md-10"> 23 @Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } }) 24 @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" }) 25 </div> 26 </div> 27 28 <div class="form-group"> 29 @Html.LabelFor(model => model.sex, htmlAttributes: new { @class = "control-label col-md-2" }) 30 <div class="col-md-10"> 31 @Html.EditorFor(model => model.sex, new { htmlAttributes = new { @class = "form-control" } }) 32 @Html.ValidationMessageFor(model => model.sex, "", new { @class = "text-danger" }) 33 </div> 34 </div> 35 36 <div class="form-group"> 37 @Html.LabelFor(model => model.age, htmlAttributes: new { @class = "control-label col-md-2" }) 38 <div class="col-md-10"> 39 @Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } }) 40 @Html.ValidationMessageFor(model => model.age, "", new { @class = "text-danger" }) 41 </div> 42 </div> 43 44 <div class="form-group"> 45 @Html.LabelFor(model => model.tel, htmlAttributes: new { @class = "control-label col-md-2" }) 46 <div class="col-md-10"> 47 @Html.EditorFor(model => model.tel, new { htmlAttributes = new { @class = "form-control" } }) 48 @Html.ValidationMessageFor(model => model.tel, "", new { @class = "text-danger" }) 49 </div> 50 </div> 51 52 <div class="form-group"> 53 <div class="col-md-offset-2 col-md-10"> 54 <input type="submit" value="Create" class="btn btn-default" /> 55 </div> 56 </div> 57 <div id="detailsID">ggggggggggg</div> 58 </div> 59 } 60 61 <div> 62 @Html.ActionLink("Back to List", "Index") 63 </div>
說明:必定要注意引入如下ajax script包,不然頁面實現不了異步方式。代碼:java
1 <script src="~/Scripts/jquery-1.10.2.min.js"></script> 2 <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> 3 <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
創建對應的Controller類,代碼以下:jquery
1 public ActionResult create() 2 { 3 return View(); 4 } 5 [HttpPost] 6 public ActionResult create(user us) 7 { 8 if (ModelState.IsValid) 9 { 10 _db.user.Add(us); 11 _db.SaveChanges(); 12 return Content("New Entry successfully added."); 13 } 14 else 15 { 16 return View(); 17 } 18 } 19 20 [HttpPost] 21 public String InsertData(user us) 22 { 23 _db.user.Add(us); 24 _db.SaveChanges(); 25 return "插入數據成功:"+us.name; 26 //return Content("New Entry successfully added."); 27 }
注意:web.config配置文件的如下2項必須爲trueweb
<add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" />