MVC+EF实现增删查改

一、Index主页(查询)

HomeController

MVCDemoEntities entity = new MVCDemoEntities();

public ActionResult Index()
{
var u = entity.Users.ToList();      ToList()是必备的,养成好这个习惯
return View(u);
}

Index.cshtml

@model IEnumerable<MVCDemo.Models.User>

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Html.ActionLink("新增", "Add")
<table>
<tr>
<th>Id</th>
<th>UserName</th>
<th>Age</th>
<th>Edit</th>
<th>Delete</th>
</tr>
@foreach (var u in Model)
{

<tr>
<td>@u.Id</td>
<td>@u.UserName</td>
<td>@u.Age</td>
<td>@Html.ActionLink("编辑", "Edit", new { id=u.Id})</td>
<td>@Html.ActionLink("删除", "Delete", new { id=u.Id})</td>      Id作为参数传递,便于删除和修改时候的查询
</tr>
}

</table>
</div>
</body>
</html>

二、新增

HomeController

public ActionResult Add()
{

return View();
}
[HttpPost]
public ActionResult Add(User u)
{
User u1 = new User();
u1.UserName=Request["UserName"];    从cshtml页面中获取值
u1.Age = Convert.ToInt32(Request["Age"]);
entity.Users.Add(u1);
entity.SaveChanges();

return RedirectToAction("Index");
}

Add.CShtml(使用的是模板)

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
<legend>User</legend>

<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>

<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}

三、修改

HomeController

[HttpGet]
public ActionResult Edit(int id)
{
//根据id查找对应的数据 然后展示
User user = entity.Users.FirstOrDefault(u => u.Id == id);

return View(user);
}
[HttpPost]
public ActionResult Edit()
{
int id =Convert.ToInt32( Request["userId"]);
User user = entity.Users.FirstOrDefault(u => u.Id == id);
user.UserName = Request["UserName"];
user.Age = Convert.ToInt32(Request["Age"]);
entity.SaveChanges();
return RedirectToAction("Index");
}

Edit.cshtml

 

<div>
@using (Html.BeginForm())
{
<table>
<tr>
<td>
@Html.LabelFor(model => model.UserName)
</td>
<td>
@Html.TextBox("UserName",Model.UserName)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Age)
</td>
<td>
@Html.TextBox("Age",Model.Age)
</td>
</tr>
<input type="hidden" name="userId" value="@Model.Id" />用隐藏的标签来存储Id
<input type="submit" name="name" value="提交" />
</table>
}

四、删除

HomeController

public ActionResult Delete(int id)
{
User user = entity.Users.FirstOrDefault(u => u.Id == id);
entity.Users.Remove(user);
entity.SaveChanges();
return RedirectToAction("Index");
}

 

 

转载于:https://www.cnblogs.com/niyingying/p/5292238.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值