AccountContext db = new AccountContext();
public ActionResult Index()
{
return View(db.SysUsers);
}
//详情
public ActionResult Details(int id)
{
SysUser sysUser = db.SysUsers.Find(id);
return View(sysUser);
}
//删除
public ActionResult Delete(int id)
{
SysUser sysUser = db.SysUsers.Find(id);
return View(sysUser);
}
[HttpPost, ActionName("Delete")]
public ActionResult ConfirmDelete(int id)
{
SysUser sysUser = db.SysUsers.Find(id);
db.SysUsers.Remove(sysUser);
db.SaveChanges();
return RedirectToAction("Index");
}
//添加用户
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(SysUser sysUser)
{
db.SysUsers.Add(sysUser);
db.SaveChanges();
return RedirectToAction("Index");
}
//修改用户
public ActionResult Edit(int id)
{
SysUser user=db.SysUsers.Find(id);
return View(user);//括号里带user是把修改的参数拿过来,展示
}
[HttpPost]
public ActionResult Edit(SysUser sysUser)
{
db.Entry(sysUser).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
-----------------------------------------------------------------------------------------------------------------------------------------
展示页面
@{
Layout = null;
}
@model IEnumerable<EF_Sql.Models.SysUser>
@*IEnumerable公开枚举器,该枚举器支持在指定类型集合上进行简单的迭代*@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table>
<tr>
<th>
@Html.DisplayNameFor(model=>model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem=>item.UserName)
</td>
<td>
@Html.DisplayFor(modelitem=>item.Email)
</td>
<td>
@Html.ActionLink("详情","Details", "Account", new { id = item.ID },null)
@Html.ActionLink("删除", "Delete", "Account", new { id = item.ID }, null)
@Html.ActionLink("修改", "Edit", "Account", new { id = item.ID }, null)
@Html.ActionLink("添加", "Create", "Account", new { id = item.ID }, null)
</td>
</tr>
}
</table>
</div>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------------------------
详情:
<!DOCTYPE html>
@model EF_Sql.Models.SysUser
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Details</title>
</head>
<body>
<div>
<dl>
<dt>@Html.DisplayNameFor(model=>model.UserName)</dt>
<dd>@Html.DisplayFor(model=>model.UserName)</dd>
<dt>@Html.DisplayNameFor(model=>model.Email)</dt>
<dd>@Html.DisplayFor(model=>model.Email)</dd>
</dl>
</div>
</body>
</html>
---------------------------------------------------------------------------
删除
@model EF_Sql.Models.SysUser
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Delete</title>
</head>
<body>
<div>
<dl>
<dt>@Html.DisplayNameFor(model => model.UserName)</dt>
<dd>@Html.DisplayFor(model => model.UserName)</dd>
<dt>@Html.DisplayNameFor(model => model.Email)</dt>
<dd>@Html.DisplayFor(model => model.Email)</dd>
</dl>
@using (Html.BeginForm())
{
<div>
<input type="submit" value="Delete" />
</div>
}
</div>
</body>
</html>
---------------------------------------------------
添加
@model EF_Sql.Models.SysUser
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>
<div>
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(model=>model.UserName)
@Html.EditorFor(model=>model.UserName)
</div>
<div>
@Html.LabelFor(model=>model.Email)
@Html.EditorFor(model=>model.Email)
</div>
<div>
@Html.LabelFor(model => model.Password)
@Html.PasswordFor(model => model.Password)
</div>
<div>
<input type="submit" value="Create" />
</div>
}
</div>
</body>
</html>
---------------------------------------------------------------------------
编辑
<title>Edit</title>
</head>
<body>
@using (Html.BeginForm())
{
@Html.HiddenFor(model=>model.ID)//相当于Web Form里的隐藏ID Hidden
<div>
@Html.LabelFor(model=>model.UserName)
@Html.EditorFor(model=>model.UserName)
</div>
<div>
@Html.LabelFor(model=>model.Password)
@Html.EditorFor(model=>model.Password)
</div>
<div>
@Html.LabelFor(model=>model.Email)
@Html.EditorFor(model=>model.Email)
</div>
<div>
<input type="submit" value="Save" />
</div>
}
</body>
</html>