MVC基础操作
C#-MVC基础操作-数据的展示及增删改、登录页面及状态保持
一、数据展示
1、View代码:
<%@Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>"%>
<%@Import Namespace="MvcApplication1.Models"%>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width"/>
<title>Main</title>
</head>
<body>
<table style="width:100%;text-align:center;"cellspacing="0" cellpadding="0" border="1">
<tr style="background-color:navy;color:white;">
<td>序号</td>
<td>用户名</td>
<td>密码</td>
<td>昵称</td>
<td>性别</td>
<td>生日</td>
<td>民族</td>
<td>班级</td>
</tr>
<%
List<Users>ulist=new UserData().Select();
foreach(Users u in ulist)
{
%>
<tr>
<td><%=u.ids %></td>
<td><%=u.username %></td>
<td><%=u.password %></td>
<td><%=u.nickname %></td>
<td><%=u.sex %></td>
<td><%=u.birthday %></td>
<td><%=u.nation %></td>
<td><%=u.@class %></td>
</tr>
<%} %>
</table>
</body>
</html>
2、页面展示
3、属性扩展性别、生日、民族、班级
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Import Namespace="MvcApplication1.Models" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width"/>
<title>Main</title>
</head>
<body>
<table style="width:100%;text-align:center;"cellspacing="0" cellpadding="0" border="1">
<tr style="background-color:navy;color:white;">
<td>序号</td>
<td>用户名</td>
<td>密码</td>
<td>昵称</td>
<td>性别</td>
<td>生日</td>
<td>民族</td>
<td>班级</td>
</tr>
<%
List<Users> ulist=new UsersData().Select();
foreach(Users u in ulist)
{
%>
<tr>
<td><%=u.ids %></td>
<td><%=u.username %></td>
<td><%=u.password %></td>
<td><%=u.nickname %></td>
<td><%=u.Convert.ToBoolean(u.sex)?"男":"女" %></td>
<td><%=Convert.ToDateTime(u.birthday).ToString("yyyy年MM月dd日")%></td>
<td><%=u.Nation1.nationname %></td>
<td><%=u.Class1.classname %></td>
</tr>
<%}%>
</table>
</html>
二、添加数据
1、设置<添加>按钮
<a href="/home/Insert">添加</a>
2、Home控制器下Insert动作代码:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>"%>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width"/>
<title>Insert</title>
</head>
<body>
<h1>添加新用户</h1>
<span>用户名:</span><input type="text"/><br/>
<span>密码:</span><input type="text"/><br/>
<span>确认密码:</span><input type="text"/><br/>
<span>昵称:</span><input type="text"/><br/>
<span>性别:</span><input type="text"/><br/>
<span>生日:</span><input type="text"/><br/>
<span>民族:</span><input type="text"/><br/>
<span>班级:</span><input type="text"/><br/>
<input type="submit" value="添加"/>
</body>
</html>
3、<添加>按钮代码 提交按钮所在的form表单
action:提交到的服务端
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>"%>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width"/>
<title>Insert</title>
</head>
<body>
<form action="/Home/Insert1" method="post">
<h1>添加新用户</h1>
<span>用户名:</span><input type="text" name="username"/><br/>
<span>密码:</span><input type="text" name="password" /><br/>
<span>确认密码:</span><input type="text" name="password1"/><br/>
<span>昵称:</span><input type="text" name="nickname"/><br/>
<span>性别:</span><input type="text" name="sex"/><br/>
<span>生日:</span><input type="text" name="birthday"/><br/>
<span>民族:</span><input type="text" name="nation"/><br/>
<span>班级:</span><input type="text" name="class" /><br/>
<input type="submit" value="添加"/>
</form>
</body>
</html>
控制器下动作代码:
//向数据库添加数据并跳转页面
public ActionResult Insert1(string username,string password,string nickname,string sex,string birthday,string nation,string @class)
{
Users u=new Users();
u.username=username;
u.password=password;
u.nickname=nickname;
u.sex=Convert.ToBoolean(sex);
u.birthday=Convert.ToDateTime(birthday);
u.nation=nation;
u.@class=@class;
using (Data0928DataContext con=new Data0928DataContext())
{
con.Users.InsertOnSubmit(u);
con.SubmitChanges();
}
return RedirectToAction("Main","Home");
}
三、删除数据(地址栏传值)
1、添加“删除”操作列
<tr>
<td><%=u.ids %></td>
<td><%=u.username %></td>
<td><%=u.password %></td>
<td><%=u.nickname %></td>
<td><%=Convert.ToBoolean(u.sex)?"男":"女" %></td>
<td><%=Convert.ToDateTime(u.birthday).ToString("yyyy年MM月dd日")%></td>
<td><%=u.Nation1.nationname %></td>
<td><%=u.Class1.classname %></td>
<td><a href="/Home/Delete?Duname=<%=u.username %>">删除</a></td>
</tr>
2、控制器动作代码
//删除数据并重新定向
public ActionResult Delete()
{
string UName=Request["Duname"];
using (Data0928DataContext con=new Data0928DataContext())
{
Users u=con.Users.Where(r=>r.username==UName).FirstOrDefault();
if(u !=null)
{
con.Users.DeleteOnSubmit(u);
con.SubmitChanges();
}
}
return RedirectToAction("Main","Home");
}
3、弹出提示框
<td><a href="/Home/Delete?Dunname=<%=u.username %>" onclick="return confirm('确定要删除吗?')">删除</a></td>
四、修改数据(路由传值+ViewBag传值)
1、路由传值-从view界面传一个值到控制器下的动作中,值为string类型
<a href="/Home/Update/<%=u.username %>修改</a>
2、ViewBag传值--从动作将数据传递到View层中进行绑定,值为任意类型
//查询到数据,用ViewBag带到界面进行绑定
public ActionResult Update(string id)
{
Users u=null;
using (Data0928DataContext con=new Data0928DataContext())
{
u=con.Users.Where(r=>r.username==id).FirstOrDefault();
}
ViewBag.UUser=u;
return View();
}
3、View层绑定数据 记得引用命名空间
<body>
<%
Users u=ViewBag.UUser as Users;
%>
<form action="/Home/Update1" method="post">
<h1>添加新用户</h1>
<span>用户名:</span><input type="text" name="username" readonly="readonly" value="<%=u.username %>"/><br/>
<span>密码:</span><input type="text" name="password" value="<%=u.password %>"/><br/>
<span>昵称:</span><input type="text" name="nickname" value="<%=u.nickname %>"/><br/>
<span>性别:</span><input type="text" name="sex" value="<%=u.sex %>"/><br/>
<span>生日:</span><input type="text" name="birthday" value="<%=u.birthday %>" /><br/>
<span>民族:</span><input type="text" name="nation" value="<%=u.nation %>"/><br/>
<span>班级:</span><input type="text" name="class" value="<%=u.@class %>"/><br/>
<input type="submit" value="确认修改"/>
</form>
</body>
4、<确认修改>按钮代码
//form表单提交数据,自动组对象,需要name属性值与字段名完全一致
public ActionResult Update1(Users u)
{
Users uu=u;
using (Data0928DataContext con=new Data0928DataContext())
{
Users uuu=con.Users.Where(r=>r.username==u.username).FirstOrDefault();
uuu.password=uu.password;
uuu.nickname=uu.nickname;
uuu.sex=uu.sex;
uuu.birthday=uu.birthday;
uuu.nation=uu.nation;
uuu.@class=uu.@class;
con.SubmitChanges();
}
return RedirectToAction("Main","Home");
}
五、登录页面及状态保持
1.新建控制器UserLoginController,并添加登录页面视图,系统是在Views文件夹中自动生成一个新文件夹
2、登录界面代码
<body>
<form actio="/UserLogin/Login1" method="post">
<h2>用户登录</h2>
用户名:<input type="text" name="UserName"/><br/>
密 码:<input type="password" name="Pwd"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
登录按钮执行控制器动作
//登录
public ActionResult Login1(string UserName,string Pwd)
{
Users u=null;
using (Data0928DataContext con=new Data0928DataContext())
{
u=con.Users.Where(r=>r.username==UserName&&r.password==Pwd).FirstOrDefault();
}
if(u==null)
{
return RedirectToAction("Login","UserLogin");
}
return RedirectToAction("Main","Home");
}
3、状态保持-cookies
Response.Coolies["UserName"].Value=UserName;
return RedirectToAction("Main","Home");
在主页面进行判断是否Cookies 为空,若为空跳回登录页面,若不为空则展示主页面内容
public ActionResult Main()
{
if(Request["UserName"]==null)
{
return RedirectToAction("Login","UserLogin");
}
return View();
}
4、退出登录
//<退出>按钮
public ActionResult Exit()
{
Response.Cookies["UserName"].Expires=DateTime.Now.AddDays(-10);
return RedirectToAction("Login","UserLogin");
}
5、登录失败提示
控制器下登录动作添加一个session 赋值
//登录
public ActionResult Login1(string UserName,string Pwd)
{
Users u=null;
using (Data0928DataContext con=new Data0928DataContext())
{
u=con.Users.Where(r=>r.username==UserName&&r.password==Pwd).FirstOrDefault();
}
if(u==null)
{
Session["LoginIsOk"]="0";
return RedirectToAction("Login","UserLogin");
}
Response.Cookies["UserName"].Value=UserName;
return RedirectToAction("Main","Home");
}
view页面进行判断
<%
if(Session["LoginIsOk"] !=null)
{
%>
<span style="color:red;">用户名或密码错误!</span>
<%
Session["LoginIsOk"]=null;
}
%>