增删改查后台页面(控制器)
using MovieProject.Entity;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
namespace MovieProject.Controllers
{
public class studentsInfoController : Controller
{
// GET: studentsInfo
MovieEntities db = new MovieEntities();//实例化虚拟数据库;
public ActionResult Index()
{
IEnumerable<studentsInfo> list = db.studentsInfo;
return View(list);
}
public ActionResult Insert()
{
return View();
}
public ActionResult add(studentsInfo model)
{
db.studentsInfo.Add(model);
int flag = db.SaveChanges();
if (flag > 0)
{
return Content("<script>alert('添加成功');window.location.href='/studentsInfo/Index'</script>");
}
else
{
return Content("<script>alert('添加失败');window.location.href='/studentsInfo/Index'</script>");
}
}
public ActionResult delete(string id)
{
studentsInfo model = db.studentsInfo.Find(Convert.ToInt32(id));
db.studentsInfo.Remove(model);
int flag = db.SaveChanges();
if (flag > 0)
{
return Content("<script>alert('删除成功');window.location.href='/studentsInfo/Index'</script>");
}
else
{
return Content("<script>alert('删除失败');window.location.href='/studentsInfo/Index'</script>");
}
}
public ActionResult Edit(string id)
{
studentsInfo model = db.studentsInfo.Find(Convert.ToInt32(id));
if (model != null)
{
return View(model);
}
else
{
return View();
}
}
public ActionResult EditStudents(studentsInfo model)
{
studentsInfo m = db.studentsInfo.Find(model.id);
if (m != null)
{
m.name = model.name;
m.age = model.age;
TryUpdateModel(model);
db.SaveChanges();
return Content("<script>alert('更新成功');window.location.href='/studentsInfo/Index'</script>");
}
else
{
return RedirectToAction("Index");
}
}
}
}
增删改查前台页面
@model IEnumerable<MovieProject.Entity.studentsInfo>//页面重定向;
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h2>学生页面</h2>
<ul>
<li>@Html.ActionLink("添加学生信息","Insert")</li>
</ul>
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>@item.name</td>
<td>@item.age</td>
<td><a href="/studentsInfo/delete/@item.id">删除</a></td>
<td><a href="/studentsInfo/Edit/@item.id">更新</a></td>
</tr>
}
</table>
</body>
</html>
添加前台页面
@model MovieProject.Entity.studentsInfo
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Insert</title>
</head>
<body>
<h2>添加学生信息</h2>
<form action="/studentsInfo/add" method="post">
<p>名字:<input type="text" name="name"/></p>
<p>年龄:<input type="text" name="age" /></p>
<p><input type="submit" value="添加"/></p>
</form>
</body>
</html>
更新前台页面
@model MovieProject.Entity.studentsInfo
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>
<h2>更新学生信息</h2>
<form action="/studentsInfo/EditStudents" method="post">
<input type="hidden" value="@Model.id" name="id"/>
<p>姓名:<input type="text" name="name" value="@Model.name"/></p>
<p>年龄:<input type="text" name="age" value="@Model.age" /></p>
<input type="submit" value="更新"/>
</form>
</body>
</html>