SQL数据库:
create database MovieDb
go
use MovieDb
create table Users
(
Uid int identity(1,1)primary key,
LoginName varchar(50)not null, --账号
Password varchar(50)not null --密码
)
go
create table MovieType(
Tid int identity(1,1) primary key, --编号
Type varchar(50) not null --类型
)
go
create table MovieInfo
(
Mid int identity(1,1) primary key, --影片编号
Name varchar(50)not null, --影片名称
Price money not null, --票价
Actor varchar(50)not null, --主演
Tid int references MovieType(Tid), --类型
TimeLong int not null --时长
)
go
insert into Users values('admin','123456')
insert into MovieType values('爱情片'),('青春片'),('武打片')
insert into MovieInfo values('成龙历险记','30','成龙','3','120'),
('少年的你','38','周冬雨','1','100'),('战狼2','50','吴京','3','140')
配置文件:
<authentication mode="Forms">
<forms loginUrl="~/Home/Users" timeout="3000"></forms>
</authentication>
登录
验证信息
登录前端页面:
@model 电影资源管理系统.Models.Users
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Users</title>
</head>
<body>
<div style="text-align:center">
<h1>管理员登录</h1>
@using (Html.BeginForm())
{
<table align="center">
<tr>
<td>账号</td>
<td>@Html.TextBoxFor(u => u.LoginName)</td>
<td style="color:red">@Html.ValidationMessageFor(u => u.LoginName)</td>
</tr>
<tr>
<td>密码:</td>
<td>@Html.TextBoxFor(u => u.Password)</td>
<td style="color:red">@Html.ValidationMessageFor(u => u.Password)</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="name" value="登录" /></td>
</tr>
<tr>
<td colspan="2" style="color:red">@Html.ValidationSummary(true)</td>
</tr>
</table>
}
</div>
</body>
</html>
Home控制器
MovieDbEntities db = new MovieDbEntities();
// GET: Home
//登录
public ActionResult Users()
{
return View();
}
[HttpPost ]
public ActionResult Users(Users u)
{
if (ModelState.IsValid && MvUser(u.LoginName,u.Password) )
{
return RedirectToAction("Index");
}
else
{
ModelState.AddModelError("","账号或密码错误");
}
return View();
}
private bool MvUser(string loginName, string password)
{
var u = (from p in db.Users where p.LoginName == loginName && p.Password == password select p).FirstOrDefault();
if (u==null)
{
return false;
}
else
{
Session["LogName"] = loginName;
FormsAuthentication.SetAuthCookie("LoginName",false);
return true;
}
查询
前端页面:
<body>
<div style="text-align:center">
<p>当前用户 :@Session["LogName"] <a href="~/Home/Users">注销</a></p>
<h1>影片信息</h1>
<form action="~/Home/Index" method="post">
电影名称:<input type="text" name="Name" value="@ViewBag.Name" />
<input type="submit" value="查询" />
</form><br />
<table border="1"cellspacing="0"align="center" width="800">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>主演</th>
<th>类型</th>
<th>时长</th>
<th>操作</th>
</tr>
@foreach (var item in ViewBag.MovInfo)
{
<tr>
<td>@item.Mid</td>
<td>@item.Name</td>
<td>@item.Price</td>
<td>@item.Actor</td>
<td>@item.Type</td>
<td>@item.TimeLong</td>
<td><a href="~/Home/AddInfoForm?Mid=@item.Mid">添加</a> <a href="DelInfo?Mid=@item.Mid" onclick="return confirm('确定删除吗')">删除</a>
<a href="~/Home/EditInfoForm?Mid=@item.Mid">修改</a>
</td>
</tr>
}
</table>
</div>
</body>
手动添加MovTypeInfo类
Home控制器
//查询
public ActionResult Index()
{
ViewBag.MovInfo = from i in db.MovieInfo
join t in db.MovieType on i.Tid equals t.Tid
select new MovTypeInfo
{
Mid=i.Mid,
Name=i.Name,
Price=i.Price,
Actor=i.Actor,
Type=t.Type,
TimeLong=i.TimeLong
};
ViewBag.Type = db.MovieType;
return View();
}
//模糊查询
[HttpPost]
public ActionResult Index(string Name)
{
//string sql = "select * from MovieInfo join MovieType on MovieInfo.Tid=MovieType.Tid where 1=1";
//ViewBag.MovInfo = db.Database.SqlQuery(typeof(MovTypeInfo), sql);
//sql += Name != "" ? "and like @Name" : "";
//SqlParameter[] ps =
//{
// new SqlParameter ("@Name",Name)
//};
if (Name != "")
{
ViewBag.MovInfo = from i in db.MovieInfo
join t in db.MovieType on i.Tid equals t.Tid
where i.Name.Contains(Name)
select new MovTypeInfo
{
Mid = i.Mid,
Name = i.Name,
Price = i.Price,
Actor = i.Actor,
Type = t.Type,
TimeLong = i.TimeLong
};
}
else
{
ViewBag.MovInfo = from i in db.MovieInfo
join t in db.MovieType on i.Tid equals t.Tid
select new MovTypeInfo
{
Mid = i.Mid,
Name = i.Name,
Price = i.Price,
Actor = i.Actor,
Type = t.Type,
TimeLong = i.TimeLong
};
}
ViewBag.Type = db.MovieType;
ViewBag.Name = Name;
return View();
}
添加
追加了添加验证:
前端页面
引用 @model 电影资源管理系统.Models.MovieInfo
<body>
<div style="text-align:center">
<h1>新增影片</h1>
<form action="AddInfoForm" method="post">
<table border="1" cellspacing="0"align="center"width="500">
<tr>
<td>名称:</td>
<td><input type="text" name="Name" value="" /></td>
<td style="color:red">@Html.ValidationMessageFor(u=>u.Name)</td>
</tr>
<tr>
<td>价格:</td>
<td><input type="text" name="Price" value="" /></td>
<td style="color:red">@Html.ValidationMessageFor(u => u.Price)</td>
</tr>
<tr>
<td>主演:</td>
<td><input type="text" name="Actor" value="" /></td>
<td style="color:red">@Html.ValidationMessageFor(u => u.Actor)</td>
</tr>
<tr>
<td>类型:</td>
<td>
<select name="Tid">
@foreach (var item in ViewBag.Type)
{
<option value="@item.Tid">@item.Type</option>
}
</select>
</td>
<td style="color:red">@Html.ValidationMessageFor(u => u.Tid)</td>
</tr>
<tr>
<td>时长:</td>
<td><input type="number" name="TimeLong" value="" /></td>
<td style="color:red">@Html.ValidationMessageFor(u => u.TimeLong)</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="新增" />
</td>
</tr>
<tr>
<td colspan="2" style="color:red">@Html.ValidationSummary(true)</td>
</tr>
</table>
</form>
</div>
</body>
Home控制器
//添加
public ActionResult AddInfoForm()
{
ViewBag.Type = db.MovieType;
return View();
}
[HttpPost]
public ActionResult AddInfoForm(MovieInfo mi)
{
if (ModelState.IsValid && isAddinfoForm(mi.Name))
{
MovieInfo mv = new MovieInfo()
{
Name = Request["Name"],
Price = int.Parse(Request["Price"]),
Actor = Request["Actor"],
Tid = int.Parse(Request["Tid"]),
TimeLong = int.Parse(Request["TimeLong"])
};
db.MovieInfo.Add(mv);
int count = db.SaveChanges();
if (count > 0)
{
return Content("<script>alert('新增成功');window.location.href='Index'</script>");
}
else
{
return Content("<script>alert('新增失败');window.location.href='AddInfoForm'</script>");
}
}
ModelState.AddModelError("","电影名称已存在");
ViewBag.Type = db.MovieType;
return View();
}
private bool isAddinfoForm(string name)
{
var u = (from p in db.MovieInfo
where p.Name == name
select p).FirstOrDefault();
if (u==null)
{
FormsAuthentication.SetAuthCookie("Name", false);
return true;
}
else
{
return false;
}
}
修改
<body>
<div style="text-align:center">
<h1>新增影片</h1>
<form action="~/Home/EditInfo" method="post">
<input type="hidden" name="Mid" value="@ViewBag.MovieInfo.Mid" />
<table border="1" cellspacing="0" align="center" width="500">
<tr>
<td>名称:</td>
<td><input type="text" name="Name" value="@ViewBag.MovieInfo.Name" /></td>
</tr>
<tr>
<td>价格:</td>
<td><input type="text" name="Price" value="@ViewBag.MovieInfo.Price" /></td>
</tr>
<tr>
<td>主演:</td>
<td><input type="text" name="Actor" value="@ViewBag.MovieInfo.Actor" /></td>
</tr>
<tr>
<td>类型:</td>
<td>
<select name="Tid">
<option value="0">--请选择--</option>
@foreach (var item in ViewBag.Type)
{
<option value="@item.Tid" @(item.Tid == ViewBag.MovieInfo.Tid?"selected":"")>@item.Type</option>
}
</select>
</td>
</tr>
<tr>
<td>时长:</td>
<td><input type="number" name="TimeLong" value="@ViewBag.MovieInfo.TimeLong" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="修改" />
</td>
</tr>
</table>
</form>
</div>
</body>
//修改
public ActionResult EditInfoForm()
{
ViewBag.Type = db.MovieType;
ViewBag.MovieInfo = db.MovieInfo.Find(int.Parse(Request["Mid"]));
return View();
}
public ActionResult EditInfo()
{
int Mid = int.Parse(Request["Mid"]);
var mv1 = db.MovieInfo.Find(Mid);
mv1.Name = Request["Name"];
mv1.Price = decimal.Parse(Request["Price"]);
mv1.Actor = Request["Actor"];
mv1.Tid = int.Parse(Request["Tid"]);
mv1.TimeLong = int.Parse(Request["TimeLong"]);
int count = db.SaveChanges();
if (count > 0)
{
return Content("<script>alert('修改成功');window.location.href='Index'</script>");
}
else
{
return Content("<script>alert('修改失败');window.location.href='Index'</script>");
}
}
删除
//删除
public ActionResult DelInfo()
{
int Mid = int.Parse(Request["Mid"]);
MovieInfo mv = db.MovieInfo.Find(Mid);
db.MovieInfo.Remove(mv);
int count = db.SaveChanges();
if (count > 0)
{
return Content("<script>alert('删除成功');window.location.href='Index'</script>");
}
else
{
return Content("<script>alert('删除失败');window.location.href='Index'</script>");
}
}