MVC数据绑定(数据的自动装载)实现数据删除和指定条件查询
数据删除:
前端视图中:
@Html.ActionLink("删除","delete","home",new {uid = item.Id},new{@class = "btn btn-danger"})
在后端控制器中:
public ActionResult delete()
{
int id = Convert.ToInt32(Request.QueryString["uid"]);
db.Users.SingeOrDefault(t=>t.Id==id);
db.Entry(u).State = System.Data.Entity.EntityState.Deleted;
db.Users.Remove(u);
db.SaveChanges();
return Content("<script>alert('确定删除吗?');window.location.href='/home/index';</script>");
return RedirectToAction("")index;
}
指定条件查询:
前端视图中:
通过姓名进行查询,将查询结果显示在主页,刷新主页内容
@using (Html.BeginForm("select","home",FormMethod.Post))
{
@Html.TextBox("name","输入姓名")
<input type="submit" value="查询" class="btn btn-success">
}
在后端控制器中:
public ActionResult select()
{
string name = Request.Form["name"];
List <User> list = db.Users.Where(t =>t.Name == name).ToList();
ViewData.Model = list;
return View("index");
}
|