Login页面前端代码
div>//表单要提交给Login页面(自己提交给自己),这样Login的后台才能取到前端页面的数据<form action=\"/First/Login\" method=\"post\">账号<input type=\"text\" name=\"zhanghao\"/><br />密码<input type=\"text\" name=\"mima\"/><br /><input type=\"submit\" value=\"登录\"/><br /></form></div>
后台代码
1) 首先要在model层 创建EF框架(访问数据库比较方便)
再model层写一个类,用于访问类
public class UserSever{//静态方法,直接调用即可public static Sysuser Login(string id,string mima) {using (fashionshoppingDBEntities db=new fashionshoppingDBEntities()){Sysuser sysuser=db.Sysuser.FirstOrDefault(s => s.username == id && s.pwd == mima);return sysuser;}}}
2)Login页面(action)的后台代码
public ActionResult Login(){return View();}[HttpPost]public ActionResult Login(string zhanghao,string mima) {Sysuser sysuser = UserSever.Login(zhanghao, mima);if (sysuser!=null){//RedirectToAction前面一定要加return 要不然无法重定向return RedirectToAction(\"About\");}else{return Content(\"<script>alert(\'账号或者密码不正确\');window.location.href=\'../First/Login\';</script>\");}// return View();}public ActionResult About() {return View();}}