MVC 注册验证参考代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcApplication5注册验证.Models { public class ZhuceBF { private mydboDataContext _Context = new mydboDataContext(); public List<zhuce> Select() { return _Context.zhuce.ToList(); } public zhuce Selectbyid(string id) { var query = _Context.zhuce.Where(p=>p.ID==id); if (query.Count()>0) { return query.First(); } return null; } public void Insert(string id,string username,string userpwd) { if (username == "" || username == " ") { } else { zhuce data = new zhuce(); data.ID = id; data.Username = username; data.Userpwd = userpwd; _Context.zhuce.InsertOnSubmit(data); _Context.SubmitChanges(); } } public void Update(string id, string username, string userpwd) { var query = _Context.zhuce.Where(p=>p.ID==id); if (query.Count()>0) { zhuce data = query.First(); data.ID = id; data.Username = username; data.Userpwd = userpwd; } _Context.SubmitChanges(); } public void Delete(string id) { var query = _Context.zhuce.Where(p=>p.ID==id); if (query.Count()>0) { zhuce data = query.First(); _Context.zhuce.DeleteOnSubmit(data); _Context.SubmitChanges(); } } } }
上边是Model里增删改查代码
这是控制器里的代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcApplication5注册验证.Models; namespace MvcApplication5注册验证.Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(); } //把值给传出来 public ActionResult Insert(string id,string username,string userpwd) { new ZhuceBF().Insert(id,username,userpwd); return RedirectToAction("Index"); } //public ActionResult Check() //{ // string idd = Session["UserId"].ToString(); // bool isok = new ZhuceBF().Selectid(idd); // Session["uid"] = isok; // return View(); //} //通过js传过来的值进行数据库查询,如果有值返回OK,没有值返回error,然后在js里进行接受 public ActionResult Check(string UserId) { zhuce user = new ZhuceBF().Selectbyid(UserId); if (user != null) return Content("error"); return Content("OK"); } //查看界面的控制器 public ActionResult chakan() { List<zhuce> list = new ZhuceBF().Select(); return View(list); } //修改界面的控制器 public ActionResult Xiugai(string id) { zhuce data = new ZhuceBF().Selectbyid(id); return View(data); } //对修改界面传值的控制器 public ActionResult Update(string id, string username, string userpwd) { new ZhuceBF().Update(id,username,userpwd); return RedirectToAction("chakan"); } public ActionResult Delete(string id) { new ZhuceBF().Delete(id); return RedirectToAction("chakan"); } } }
这是主视图的代码,注册界面
@using MvcApplication5注册验证.Models; @using MvcApplication5注册验证.Controllers; @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <style type="text/css"> .tr1 { background-color:blue; text-align:center; font-weight:bold; } .tr2 { background-color:#FFFFCC; text-align:center; } </style> </head> <body> <div id="tishi"></div> <center><h2>注册新用户</h2></center> <center> <form action="/Home/Insert" method="post"> <table width="60%" cellpadding="5" cellspacing="1" border="0"> <tr class="tr1"> <td>用户名ID:</td> <td> <input type="text" name="id" id="UserId" > <input id="Button1" type="button" onclick="yanzheng()" value="唯一验证" /><br/> <div id="me"></div> </td> </tr> <tr class ="tr2"> <td>用户名:</td> <td><input id="Text2" type="text" name="username" /></td> </tr> <tr class="tr2"> <td>密码</td> <td><input id="Text3" type="password" name="userpwd"/></td> </tr> <tr class ="tr2"> <td>确认密码</td> <td><input id="Text4" type="password" /></td> </tr> <tr class="tr2"> <td"> </td> </tr> </table> <input id="Submit1" type="submit" value="注册" onclick="return checkuid()"/> <a href="/Home/chakan"><input id="Button2" type="button" value="查看" /></a> <form/> </center> </body> </html> <script type="text/javascript" src="js/JavaScript1.js"></script> <script> function checkuid() { var a = document.getElementById("Text2"); var b = document.getElementById("tishi"); var c=document.getElementById("Text3"); var d=document.getElementById("Text4"); var txt1=c.value; var txt2=d.value; var x = a.value; if (x == "" || x == " ") { b.innerHTML = "用户名不能为空"; return false; } else if(txt1!=txt2) { b.innerHTML = "两次密码输入不一样!请重新输入"; return false; } else { b.innerHTML = "恭喜你注册成功!"; return true; } } </script> <script type="text/javascript"> function yanzheng() { var userName = document.getElementById("UserId").value; ajax("/Home/Check?UserId=" + userName, function (resText) { if (resText == "OK") { document.getElementById("me").innerHTML = "有效用户ID"; } else if (resText == "error") { document.getElementById("me").innerHTML = "无效用户ID"; } }); } </script>
这是查看的视图代码
@using MvcApplication5注册验证.Models; @using MvcApplication5注册验证.Controllers; @model List<zhuce> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>chakan</title> <style type="text/css"> .tr1 { background-color:blue; text-align:center; font-weight:bold; } .tr2 { background-color:#FFFFCC; text-align:center; } </style> </head> <body> <center><table width="60%" cellpadding="5" cellspacing="1" border="0"> <tr class="tr1"> <td>用户ID</td> <td>用户名</td> <td>密码</td> <td>操作</td> </tr> @foreach(zhuce data in Model){ <tr class="tr2"> <td>@data.ID</td> <td>@data.Username</td> <td>@data.Userpwd</td> <td> <a href="/Home/Xiugai/@data.ID">修改</a> <a href="/Home/Delete/@data.ID">删除</a> </td> </tr> } </table></center> <center><a href="/Home/Index ">返回</a></center> </body> </html>
这是修改的视图代码
@using MvcApplication5注册验证.Models; @using MvcApplication5注册验证.Controllers; @model zhuce @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title></title> <style type="text/css"> .tr1 { background-color:#FFFFCC; text-align:center; font-weight:bold; } </style> </head> <body> @using (Html.BeginForm("Update","Home",FormMethod.Post)){ <center><table width="60%" cellpadding="5" cellspacing="1" border="0"> <tr class="tr1"> <td>用户ID</td> <td><input id="Text1" type="text" name="id" value=" @Model.ID" /></td> </tr> <tr class="tr1"> <td>用户名</td> <td><input id="Text2" type="text" name="username" value="@Model.Username"/></td> </tr> <tr class="tr1"> <td>密码</td> <td><input id="Text3" type="text" name="userpwd" value=" @Model.Userpwd"/></td> </tr> </table></center> <center><input id="Submit1" type="submit" value="提交" /> <a href="/Home/Index"><input id="Button1" type="button" value="返回" /><a></center> } </body> </html>