Search code examples
asp.net-mvcentity-frameworkasp.net-identity

How to update my user account data on ASP.net MVC 5 application


I am new to the asp.net MVC 5 identity framework andI am try to do update my details directly. Straight forward, What I want to do is to update my user information to the database. Previously, I changed my user details by using Migrations and I use entity framework in order to generate my controller, view and model it self. However, How do I update my user details. I have seen role methods..but I never understand, How can I do? without using role..Because, I want to update all of user information that I needed to do it in UserManageController...

Is it possible? in a different controller and getting values directly on generated user account? How to retrieve then?

Here is my Identity Models

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public string userFname { get; set; }
        public string userLname { get; set; }
        public string address { get; set; }
        public string userContactNo { get; set; }
        public string commercialName { get; set; }
        public string commercialAddress { get; set; }
        public string commercialEmail { get; set; }
        public string userType { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

Here is my Registeration model

public class RegisterViewModel
{
    [Required]
    [Display(Name = "User First Name")]
    public string userFname { get; set; }

    [Required]
    [Display(Name = "User Last Name")]
    public string userLname { get; set; }

    [Required]
    [Display(Name = "User Address")]
    public string address { get; set; }

    [Required]
    [Display(Name = "User Contact Number")]
    public string userContactNo { get; set; }

    [Display(Name = "Commercial Name")]
    public string commercialName { get; set; }

    [Display(Name = "Commercial Address")]
    public string commercialAddress { get; set; }

    [EmailAddress]
    [Display(Name = "Commercial Email")]
    public string commercialEmail { get; set; }

    [Key]
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]        
    public string userType { get; set; }

}

Solution

  • How I do this,

    UPDATED: As he commented in my answer bellow, he want to update a list of users in the same method. So this will work.

    [HttpPost]
    public async Task<ActionResult> UpdateUserInfo(List<RegisterViewModel> model)
    {
      if (!ModelState.IsValid)
        {
            return View(model);
        }
    
        var userStore = new UserStore<ApplicationUser>(new 
                                      ApplicationDbContext());
        var appManager = new UserManager<ApplicationUser>(userStore);
    
        // here you can do a foreach loop and get the email and assign new datas
        foreach(var i in model)
         {
           var currentUser = appManager.FindByEmail(i.Email);
    
           // here you can assign the updated values
           currentUser.userFname = i.userFname;
           // and rest fields are goes here
           await appManager.UpdateAsync(currentUser);
         }
        var ctx = userStore.Context;
        ctx.SaveChanges();
        // now you can redirect to some other method or-else you can return 
        // to this view itself by returning the data
    
        return RedirectToAction("SomeActionMethod");
    }
    

    And yes, you should have the fields in your view and there will be a @Html.BeginForm and a submit button to post your data. Or-else you can post by ajax method

    Hope it helps.