VS2015+MVC5+AJAX+Sqlserver环境搭建

  1. 利用ado.net添加数据库实体类,而且生成user实体类。
  2. 添加Controller类。
  3. 添加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 Code

  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>
View Code

  说明:必定要注意引入如下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>
View Code

  创建对应的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         }
View Code

   注意:web.config配置文件的如下2项必须为trueweb

  <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
相关文章
相关标签/搜索