public IEnumerable<System.Web.Mvc.SelectListItem> GetProjectStageList() { using (var db = new ProjectsContext()) { var tempList = db.ProjectStages.Select(a => new MyKeyValue { Key = a.ID, Value = a.Name }); List<System.Web.Mvc.SelectListItem> selectList = new List<System.Web.Mvc.SelectListItem>(); foreach (var pair in tempList) { selectList.Add(new System.Web.Mvc.SelectListItem { Text = pair.Value, Value = pair.Key.ToString() }); } return selectList; } }
public ActionResult Register(RegisterModel model) { using (var db = new ProjectsContext()) { if (!ModelState.IsValidField("UserDepartment")) { // 设置部门 var departmentId = int.Parse(Request.Params["UserDepartment"]); var userDepartment = db.UserDepartments.First(d => d.DepartmentId == departmentId); if (userDepartment == null) { ModelState.AddModelError("UserDepartment", "部门模型转换失败"); } else { model.UserDepartment = userDepartment; ModelState.Remove("UserDepartment"); } } } using (var db = new UsersContext()) { if (ModelState.IsValid) { // 尝试注册用户 try { WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { RealName = model.RealName }, true); var userProfile = db.UserProfiles.First(d => d.UserName == model.UserName); userProfile.UserDepartment = model.UserDepartment; db.SaveChanges(); //WebSecurity.Login(model.UserName, model.Password); //return RedirectToAction("Index", "Home"); ModelState.AddModelError("", "账号已创建,请等待系统管理员激活。"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // 如果我们进行到这一步时某个地方出错,则重新显示表单 return View(model); } }