VS2015+MVC5+AJAX+Sqlserver环境搭建

  1. 利用ado.net添加数据库实体类,并且生成user实体类。
  2. 添加Controller类。
  3. 添加View视图。

  实体数据添加不做说明,user代码如下:

 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文件,代码如下:

 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包,否则页面实现不了异步方式。代码:

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类,代码如下:

 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项必须为true

  <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 

转载于:https://www.cnblogs.com/jianzi/p/9837359.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值