예제 #1
0
        public ActionResult Create(EditMemberViewModel model)
        {
            if (ModelState.IsValid)
            {
                Member member = model.ConvertToEntity();
                member.MemberSince = DateTime.Now;

                Login login = null;
                if (model.LoginId == 0)
                {
                    login = _dataService.GetLoginByUsername(model.Username);
                }
                else
                {
                    login = _dataService.GetLoginById(model.LoginId);
                }

                if (login == null)
                {
                    ModelState.AddModelError("Username", String.Format("Username '{0}' can not be found in the database", model.Username));
                    model.ClubRoles = _dataService.GetAllRoles();
                    return(View(ViewNames.CreateMember, model));
                }

                member.LoginId = login.Id;

                if (model.MemberRoles != null && model.MemberRoles.Count > 0)
                {
                    List <Role> allRoles = _dataService.GetAllRoles();
                    member.Roles = new List <Role>();
                    foreach (int roleId in model.MemberRoles)
                    {
                        member.Roles.Add(allRoles.FirstOrDefault(r => r.Id == roleId));
                    }
                }

                member.Status = model.Status;
                _dataService.SaveMember(member);
                return(RedirectToAction("Details", new { id = member.Id }));
            }

            model.MemberRoles = new List <int>();
            model.ClubRoles   = _dataService.GetAllRoles();
            return(View(ViewNames.EditMember, model));
        }