在<head>引用
在<body>编辑HTML
//指定提交到Home控制器下的Login方法 //HttpMethod 指定提交方式为Post //OnSuccess 返回方法为AfterAdd //id id名称为frmSet //type="submit" 按钮类型为 submit @using (Ajax.BeginForm("Login","Home", new { }, new AjaxOptions() { HttpMethod = "Post", OnSuccess = "AfterAdd" }, new { id = "frmSet" })) {}
Home 控制器 Login方法
[HttpPost] public ActionResult Login(FormCollection form) { string msg = ""; string usernmae = Common.Tool.GetSafeSqlandHtml(Request ["username"]); string pwd = Common.Tool.GetSafeSqlandHtml(Request["pwd"]); string checkCode = Common.Tool.GetSafeSqlandHtml(Request["CheckCode"]); if (Session["ValidataCode"] == null || checkCode != Session["ValidataCode"].ToString()) { return Json(new { isok = "erro", msg = "验证码错误" }); } if (string.IsNullOrEmpty(usernmae) || string.IsNullOrEmpty(pwd)) { return Json(new { isok = "erro", msg = "用户名或密码不能为空" }); } pwd = Common.Tool.Md5(pwd).ToUpper(); MODEL.User us = BLL.User.SelectModel("userName='" + usernmae + "' and PassWord='" + pwd + "' "); if (us == null) { msg = "用户名或密码错误"; } else { if (us.Status == 1) { return Json(new { isok = "erro", msg = "该管理员已被禁用" }); } else { MODEL.LoginLog ulogin = new MODEL.LoginLog(); ulogin.Uid = us.Id; ulogin.UserID = us.UserName; ulogin.UserName = us.Name ; ulogin .IP = Common .Tool .GetClientIp (); System.Web.HttpContext Current = System.Web.HttpContext.Current; ulogin.LoginWeb = Current.Request.ServerVariables["HTTP_USER_AGENT"]; ulogin.AddTime = DateTime.Now; BLL.LoginLog.Add(ulogin); Current.Session["User"] = us; string goUrl = "/Cms/Default/Index"; return Json(new { isok = "ok", gourl = goUrl }); } } return Json(new { isok = "erro", msg = msg }); }
编辑返回方法AfterAdd
最后,完成!