目录
数据库
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','admin'),('admin2','admin2'),('admin3','admin3')
GO
insert into MovieType VALUES('动漫类'),('爱情类'),('武装类')
GO
insert into MovieInfo values('长津湖',59,'吴京',3,20000),('熊出没',49,'张三',1,20000),('我真的讨厌异地恋',59,'李四',2,20000)
登陆
页面代码
//引用模型类
//验证
@model Asp.Net_MVC.Models.Users
<body>
<div>
<h3 style="text-align:center;">管理员进入</h3>
@using(Html.BeginForm())
{
<table cellspacing="0" style="text-align:center" 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.PasswordFor(u => u.Password)</td>
<td style="color:red">@Html.ValidationMessageFor(u => u.Password)</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>
}
</div>
</body>
控制器代码
MovieDbEntities db = new MovieDbEntities();
//登录页面
public ActionResult LoginON()
{
return View();
}
//登录
[HttpPost]
public ActionResult LoginON(Users u)
{
if (ModelState.IsValid && UserValied(u.LoginName, u.Password))
{
//Session["name"] = u.LoginName;
return Content("<script>alert('登陆成功');window.location.href='Index'</script>");
}
else
{
ModelState.AddModelError("", "账号或者密码错误");
return View();
}
}
//访问数据库验证用户登录信息并使用Forms验证
private bool UserValied(string name, string password)
{
var sql = (from u in db.Users where u.LoginName == name && u.Password == password select u).FirstOrDefault();
if (sql == null)
{
return false;
}
else
{
FormsAuthentication.SetAuthCookie(name, false);
Session["name"] = name;
return true;
}
}
非空验证块儿
过滤器配置文件
<authentication mode="Forms">
<forms loginUrl="~/Home/LoginON" timeout="3000"></forms>
</authentication>
主页面
主页面代码
<body>
<div>
当前用户: @Session["name"] <a href="~/Home/LogOut">注销</a>
<h1 style="text-align:center">影片信息</h1>
<form action="/" method="post">
<div style="margin:auto;width:800px;text-align:center">
电影名称:<input type="text" name="name" value="@ViewBag.Moviename" />
<input type="submit" name="name" value="查询" />
</div>
</form>
<table border="1" cellspacing="0" align="center" style="text-align:center">
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
<td>主演</td>
<td>类型</td>
<td>时长</td>
<td>操作</td>
</tr>
@foreach (var item in ViewBag.MovieList)
{
<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/AddMovieForm">添加</a>|<a href="deleInfo?Mid=@item.Mid" onclick="return confirm('确定删除吗?')">删除</a>|<a href="/Home/ExitInfoForm?Mid=@item.Mid">修改</a></td>
</tr>
}
</table>
</div>
</body>
控制器代码
//主页面
[Authorize]
public ActionResult Index()
{
ViewBag.MovieList = from m in db.MovieInfo
join n in db.MovieType on m.Tid equals n.Tid
select new NewMovieInfo
{
Mid = m.Mid,
Actor = m.Actor,
Name = m.Name,
Price = m.Price,
TimeLong = m.TimeLong,
Type = n.Type
};
return View();
}
模糊查询
//模糊查询
[HttpPost]
public ActionResult Index(string name)
{
if (name != "")
{
ViewBag.MovieList = from m in db.MovieInfo
join n in db.MovieType on m.Tid equals n.Tid
where m.Name.Contains(name)
select new NewMovieInfo
{
Mid = m.Mid,
Actor = m.Actor,
Name = m.Name,
Price = m.Price,
TimeLong = m.TimeLong,
Type = n.Type
};
}
else
{
ViewBag.MovieList = from m in db.MovieInfo
join n in db.MovieType on m.Tid equals n.Tid
select new NewMovieInfo
{
Mid = m.Mid,
Actor = m.Actor,
Name = m.Name,
Price = m.Price,
TimeLong = m.TimeLong,
Type = n.Type
};
}
ViewBag.Moviename = name;
return View();
}
添加
添加页面
//表单非空验证引用
@model Asp.Net_MVC.Models.MovieInfo
<body>
<div>
<form action="/Home/AddMovieForm" method="post">
<h1 style="text-align:center">新增影片</h1>
<table cellspacing="0" align="center" style="text-align:center">
<tr>
<td>名称</td>
<td><input type="text" name="Name" value="" /></td>
<td style="color:red">@Html.ValidationMessageFor(m => m.Name)</td>
</tr>
<tr>
<td>价格</td>
<td><input type="text" name="Price" value="" /></td>
<td style="color:red">@Html.ValidationMessageFor(m => m.Price)</td>
</tr>
<tr>
<td>主演</td>
<td><input type="text" name="Actor" value="" /></td>
<td style="color:red">@Html.ValidationMessageFor(m => m.Actor)</td>
</tr>
<tr>
<td>类型</td>
<td>
<select name="Type">
@foreach (var item in ViewBag.TypeList)
{
<option value="@item.Tid">@item.Type</option>
}
</select>
</td>
</tr>
<tr>
<td>时长</td>
<td><input type="text" name="TimeLong" value="" /></td>
<td style="color:red">@Html.ValidationMessageFor(m => m.TimeLong)</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="新增" />
</td>
</tr>
<tr>
<td colspan="3" style="color:red;">
@Html.ValidationSummary(true)
</td>
</tr>
</table>
</form>
</div>
</body>
控制器代码
//添加页面
[Authorize]
public ActionResult AddMovieForm()
{
ViewBag.TypeList = db.MovieType;
return View();
}
//添加
[HttpPost]
public ActionResult AddMovieForm(MovieInfo m)
{
ViewBag.TypeList = db.MovieType;
if (ModelState.IsValid)
{
MovieInfo mov = new MovieInfo()
{
Name = Request["Name"],
Actor = Request["Actor"],
Price = decimal.Parse(Request["Price"]),
Tid = int.Parse(Request["Type"]),
TimeLong = int.Parse(Request["TimeLong"])
};
db.MovieInfo.Add(mov);
}
if (MovieName(m.Name))
{
int count = db.SaveChanges();
if (count > 0)
{
return Content("<script>alert('添加成功');window.location.href='Index'</script>");
}
}
return View();
}
//验证方法
private bool MovieName(string Name)
{
var sql = (from m in db.MovieInfo where m.Name == Name select m).FirstOrDefault();
if (sql == null)
{
return true;
}
else
{
ModelState.AddModelError("", "该电影名称已存在,请别再叭叭");
return false;
}
}
验证块
修改
页面
页面代码
<body>
<div>
<form action="/Home/ExitInfo" method="post">
<h1 style="text-align:center">修改影片</h1>
<input type="hidden" name="Mid" value="@ViewBag.MovieInfo.Mid" />
<table border="1" cellspacing="0" align="center" style="text-align:center">
<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="Type">
<option value="0">--请选择--</option>
@foreach (var item in ViewBag.TypeList)
{
<option value="@item.Tid" @(item.Tid == ViewBag.MovieInfo.Tid ? "selected" : "")>@item.Type</option>
}
</select>
</td>
</tr>
<tr>
<td>时长</td>
<td><input type="text" name="TimeLong" value="@ViewBag.MovieInfo.TimeLong" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="修改" />
</td>
</tr>
</table>
</form>
</div>
</body>
控制器代码
通过id获取某一条信息
//修改页面
[Authorize]
public ActionResult ExitInfoForm()
{
ViewBag.TypeList = db.MovieType;
ViewBag.MovieInfo = db.MovieInfo.Find(int.Parse(Request["Mid"]));
return View();
}
//修改
public ActionResult ExitInfo()
{
MovieInfo m = db.MovieInfo.Find(int.Parse(Request["Mid"]));
m.Name = Request["Name"];
m.Actor = Request["Actor"];
m.Price = decimal.Parse(Request["Price"]);
m.Tid = int.Parse(Request["Type"]);
m.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 deleInfo()
{
//通过主键查找信息
int Mid = int.Parse(Request["Mid"]);
ViewBag.Mid = db.MovieInfo.Find(Mid);
db.MovieInfo.Remove(ViewBag.Mid);
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 LogOut()
{
//退出
FormsAuthentication.SignOut();
Session.Abandon();
return RedirectToAction("LoginON");
}