示例#1
0
        public ActionResult Register(RegisterViewModel model)
        {
            // Check if there are roles in the database
            var roles = this.UnitOfWork.RoleRepository.Get().Count();
            if (roles == 0)
            {
                var modl = new ReUzze.Models.RegisterViewModel
                {
                    StatusMessage = "There are no roles in the database ...",
                    Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                };
                return View(modl);
            }

            // Check if there are regions in database
            var regions = this.UnitOfWork.RegionRepository.Get().Count();
            if (regions == 0)
            {
                var modl = new ReUzze.Models.RegisterViewModel
                {
                    StatusMessage = "There are no regions in the database ...",
                    Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                };
                return View(modl);
            }

            // Check if user has selected region
            if (model.SelectRegionId == 0)
            {
                var modl = new ReUzze.Models.UserViewModel
                {
                    StatusMessage = "You need to select a region!",
                    Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                };
                return View(modl);
            }

            // Check if username already exists
            var usrname = this.UnitOfWork.UserRepository.Get().Where(u => u.UserName == model.UserName).Count();
            if (usrname != 0)
            {
                var modl = new ReUzze.Models.RegisterViewModel
                {
                    StatusMessage = "Please choose another username. This one already exists.",
                    Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                };
                return View(modl);
            }

            // Check if user email already exists
            var email = this.UnitOfWork.UserRepository.Get().Where(u => u.Email == model.Email).Count();
            if (email != 0)
            {
                var modl = new ReUzze.Models.RegisterViewModel
                {
                    StatusMessage = "This email address is already registered.",
                    Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                };
                return View(modl);
            }

            if (!ModelState.IsValid)
            {
                model = new ReUzze.Models.RegisterViewModel
                {
                    Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                };

                return View(model);
            }

            else if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    MembershipService.CreateUser(model.City, model.Street, model.StreetNumber, Convert.ToDecimal(model.Latitude), Convert.ToDecimal(model.Longitude), model.SelectRegionId, model.FirstName, model.SurName, model.UserName, model.Email, model.Password);
                    FormsAuthentication.SetAuthCookie(model.UserName, false);

                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            return View(model);
        }
示例#2
0
        public ActionResult Register()
        {
            var model = new ReUzze.Models.RegisterViewModel
            {
                Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
            };

            return View(model);
        }