public ViewResult EditUser(string userId)
 {
     User u = !string.IsNullOrEmpty(userId) ? UserRepository.GetUser(userId) : new User();
     AdminUserModel m = new AdminUserModel()
     {
         UserId = userId,
         Name = u.Name,
         Email = u.Email,
         FullName = !string.IsNullOrEmpty(u.FullName) ? u.FullName : null,
         Uri = u.Uri != null ? u.Uri.ToString() : null,
         Ids = u.Ids != null ? string.Join(Environment.NewLine, u.Ids.ToArray()) : null,
         Password = u.Password
     };
     if (TempData["success"] != null)
     {
         m.Notifications.Add("Saved!", "The user data was saved successfully.");
     }
     return View("AdminUser", "Admin", m);
 }
        public ActionResult EditUser(AdminUserModel m)
        {

            if (string.IsNullOrEmpty(m.Name) || m.Name.Trim().Length == 0)
            {
                this.ModelState.AddModelError("name", "The username is required.");
            }

            if (string.IsNullOrEmpty(m.Email) || m.Email.Trim().Length == 0)
            {
                this.ModelState.AddModelError("email", "The email is required.");
            } //TODO: check valid email format

            if (string.IsNullOrEmpty(m.Ids) || m.Ids.Trim().Length == 0)
            {
                this.ModelState.AddModelError("ids", "You must supply at least one Id.");
            }

            if (!string.IsNullOrEmpty(m.Password) && m.Password != m.ConfirmPassword)
            {
                this.ModelState.AddModelError("confirmPassword", "The passwords don't match.  Please reconfirm.");
            }

            if (!string.IsNullOrEmpty(m.Uri) && !Uri.IsWellFormedUriString(m.Uri, UriKind.Absolute))
            {
                this.ModelState.AddModelError("uri", "The website address you've entered is not correct.");
            }

            if (this.ModelState.IsValid)
            {
                //TODO: support both ajax and full page
                try
                {
                    bool isNew = true;
                    User u = new User();
                    if (!string.IsNullOrEmpty(m.UserId))
                    {
                        u = UserRepository.GetUser(m.UserId);
                        if (u == null) throw new Exception("Can't find user to modify.");
                        else isNew = false;
                    }
                    u.Ids = m.Ids.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Select(id => id.Trim());
                    u.Name = m.Name;
                    u.FullName = m.FullName;
                    u.Email = m.Email;
                    if (!string.IsNullOrEmpty(m.Uri)) u.Uri = new Uri(m.Uri);
                    if (!string.IsNullOrEmpty(m.Password)) u.Password = m.Password;
                    m.UserId = u.Ids.First();

                    if (isNew) UserRepository.CreateUser(u);
                    else UserRepository.UpdateUser(u);

                    TempData["success"] = true;
                    return RedirectToAction("EditUser", new { userId = m.UserId });
                }
                catch (Exception ex)
                {
                    m.Errors.Add(ex.Message);
                }
            }
            else
            {
                m.Errors.Add("");
            }

            return View("AdminUser", "Admin", m);
        }