MVC简单的增删改查

MVC简单的增删改查

最近的学习了一下mvc,现在做一个mvc的CRUD例子。

1、创建实体模型

MVC简单的增删改查_第1张图片

2、创建一个UserInfo的控制器

MVC简单的增删改查_第2张图片

 MVC简单的增删改查_第3张图片

3、查询数据

 public IList userInfoList { get; set; }
         DemoEntities demoEntities = new DemoEntities();//创建EF上下文
         public ActionResult Index()
         {
             userInfoList = demoEntities.Set().ToList();
             ViewBag.userInfoList = userInfoList;
             return View();
         }

view

  @{
      Layout = null;
  }
  @using MVCCRUDAPI.Models;
  
  
  
  
     
     Index
 
 
     
         @foreach (UserInfo userInfo in ViewBag.userInfoList)
         {
             
         }
     
用户名密码年龄邮箱备注添加时间操作
@userInfo.UserName@userInfo.UserPwd@userInfo.Age@userInfo.Email@userInfo.Remark@userInfo.RegTime编辑 删除

数据显示

MVC简单的增删改查_第4张图片

3、添加数据

code

  public ActionResult Add()
         {
             return View();
         }
         [HttpPost]
         public ActionResult Add(UserInfo userInfo)
         {
             if (!ModelState.IsValid)
             {
                 return Content("error");
             }
             userInfo.RegTime = DateTime.Now;
             demoEntities.UserInfo.Add(userInfo);
             demoEntities.SaveChanges();
             return Content("ok");
        }

view

 

 @{
     Layout = null;
 }
 @model MVCCRUDAPI.Models.UserInfo
 
 
 
 
     
     添加
     
     
     
     
     
 
 
     
@using (Ajax.BeginForm("add", "UserInfo", new AjaxOptions { HttpMethod = "post", OnSuccess = "success" })) {
@Html.LabelFor(u => u.UserName)
@Html.TextBoxFor(u => u.UserName)
@Html.LabelFor(u => u.UserPwd)
@Html.PasswordFor(u => u.UserPwd)
@Html.LabelFor(u => u.Email)
@Html.TextBoxFor(u => u.Email)
@Html.LabelFor(u => u.Age)
@Html.TextBoxFor(u => u.Age)
@Html.LabelFor(u => u.Remark)
@Html.TextAreaFor(u => u.Remark)

返回 }

复制代码

4、更新数据

code

复制代码

 1 UserInfo userInfo = new UserInfo();
 2 public ActionResult Edit(int id)
 3         {
 4             userInfo = demoEntities.Set().Where(u => u.Id == id).FirstOrDefault();
 5             return View(userInfo);
 6         }
 7         [HttpPost]
 8         public ActionResult Edit(UserInfo userInfo)
 9         {
10             if (!ModelState.IsValid)
11             {
12                 return Content("error");
13             }
14             demoEntities.UserInfo.Attach(userInfo);//对象添加到EF管理容器中
15             demoEntities.Entry(userInfo).State = EntityState.Modified;
16             demoEntities.SaveChanges();
17             return Content("ok");
18         }

复制代码

veiw

复制代码

 1 @model MVCCRUDAPI.Models.UserInfo
 2 
 3 @{
 4     Layout = null;
 5 }
 6 
 7 
 8 
 9 
10 
11     
12     编辑
13 
14 
15     
16     
17     
18     
19     
30     @using (Ajax.BeginForm("Edit", "UserInfo", new AjaxOptions { OnSuccess = "success", HttpMethod = "post" }))
31     {
32         @Html.HiddenFor(model => model.Id)
33 
34         
35 @Html.LabelFor(model => model.UserName) 36
37
38 @Html.EditorFor(model => model.UserName) 39 40
41
42 @Html.LabelFor(model => model.UserPwd) 43
44
45 @Html.EditorFor(model => model.UserPwd) 46 47
48
49 @Html.LabelFor(model => model.Age) 50
51
52 @Html.EditorFor(model => model.Age) 53 54
55
56 @Html.LabelFor(model => model.Email) 57
58
59 @Html.EditorFor(model => model.Email) 60 61
62
63 @Html.LabelFor(model => model.Remark) 64
65
66 @Html.TextAreaFor(u => u.Remark) 67 68
69 @Html.HiddenFor(model => model.RegTime) 70

71 返回 72

73 } 74 75

复制代码

5、删除数据

code

复制代码

1 public ActionResult Delete(int id)
2         {
3             userInfo.Id = id;
4             demoEntities.UserInfo.Attach(userInfo);//对象添加到EF管理容器中
5             demoEntities.Entry(userInfo).State = EntityState.Deleted;
6             demoEntities.SaveChanges();
7             return Content("ok");
8         }

复制代码

view 在index页面加入下面的js代码即可

复制代码

 1 

复制代码

你可能感兴趣的:(asp.net)