public ActionResult Create(User value) { try { // TODO: Add insert logic here value.USERSTATE = "1"; db.User.Add(value); db.SaveChanges(); return RedirectToAction("Index"); } catch (Exception ex) { return Content(string.Format("<script >alert('添加失败,错误信息:{0}');window.history.go( -1 ); </script >", ex.Message), "text/html"); } }
public ActionResult Validate(User param) { if (string.IsNullOrEmpty(param.USERNAME) || string.IsNullOrEmpty(param.PASSWORD)) { return Content("<script >alert('用户名或者密码不能为空,请输入!');window.history.go( -1 ); </script >", "text/html"); } User model =db.User.Where(u => u.USERNAME.Trim() == param.USERNAME.Trim() && u.PASSWORD.Trim() == param.PASSWORD.Trim()).SingleOrDefault(); if (model == null) { return Content("<script >alert('登录失败,用户名或者密码错误,请输入!');window.history.go( -1 ); </script >", "text/html"); } if (model.USERSTATE!= null && model.USERSTATE!="1") { return Content("<script >alert('登录失败,该用户已经禁用,不能登录系统!');window.history.go( -1 ); </script >", "text/html"); } //HttpContext.Application.Add("CurrentUser", model.USERNAME); //HttpContext.Application.Set("CurrentUser-" + model.USERID, model.USERNAME); //HttpContext.Items.Add("CurrentUser", model.USERNAME); HttpCookie _cookie = new HttpCookie("user"); _cookie.Values.Add("UserName", model.USERID.ToString()); Response.Cookies.Add(_cookie); FormsAuthentication.SetAuthCookie(model.USERID.ToString(), true); FormsAuthentication.RedirectFromLoginPage(model.USERNAME.Trim(), true); return Redirect("/"); }
public ActionResult Edit(User entity) { try { db.Entry(entity).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); var items=db.Customer.Where(c => c.Owner == entity.USERID && c.DepartmentID!=entity.DEPARTMENTID); foreach (var item in items) { item.DepartmentID = entity.DEPARTMENTID; db.Entry(item).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } catch (Exception ex) { return Content(string.Format("<script >alert('编辑失败,错误信息:{0}');window.history.go( -1 ); </script >", ex.Message), "text/html"); } return Content("<script >alert('编辑成功!');window.opener=null;window.close(); </script >", "text/html"); }