Update ApplicationUser data

0

I have problems with the following method.

public async Task<string> EditUsuario(
            string id, 
            string userName, 
            string email, 
            string phoneNumber, 
            int accessFailedCount, 
            string concurrencyStamp,
            bool emailConfirmed, 
            bool lockoutEnabled, 
            DateTimeOffset lockoutEnd, 
            string normalizedEmail, 
            string normalizedUserName, 
            string passwordHash, 
            bool phoneNumberConfirmed, 
            string securityStamp, 
            bool twoFactorEnabled, 
            ApplicationUser applicationUser
        )
        {
            var resp = "";
            try
            {
              applicationUser = new ApplicationUser
              {
                  Id = id,
                  UserName = userName,
                  Email = email,
                  PhoneNumber = phoneNumber,
                  EmailConfirmed = emailConfirmed,
                  LockoutEnabled = lockoutEnabled,
                  LockoutEnd = lockoutEnd,
                  NormalizedEmail = normalizedEmail,
                  NormalizedUserName = normalizedUserName,
                  PasswordHash = passwordHash,
                  PhoneNumberConfirmed = phoneNumberConfirmed,
                  SecurityStamp = securityStamp,
                  TwoFactorEnabled = twoFactorEnabled,
                  AccessFailedCount = accessFailedCount,
                  ConcurrencyStamp = concurrencyStamp
              };
              //Actualizamos los datos
              _context.Update(applicationUser);
              await _context.SaveChangesAsync();
              resp = "Save";
            }
            catch
            {
              resp = "No Save";
            }
            return resp;
        }

I should update the data, but resp always returns "No Save". I have no idea what exactly is failing. Would anyone know how to tell me?

Greetings and thanks

    
asked by loff 02.04.2018 в 19:38
source

1 answer

0

Always remember that you want to Edit a User, so you need first of all to have that user, in your example you are creating a new one. I'll give you an example:

public class TuControlador : Controller
{
    private readonly UserManager<ApplicationUser> _userManager;
    public TuControlador(UserManager<ApplicationUser> userManager){
      _userManager = userManager;
     }

[HttpPost]
public async Task<string> EditUsuario(string Id, ApplicationUser applicationUser){
    var user = await _userManager.FindByIdAsync(Id);
    if(user == null){
      return NotFound();
      }
    user.Email = applicationUser.Email;
    user.PhoneNumber = applicationUser.PhoneNumber;
    ....
    try{
     await _userManager.UpdateAsync(user);
     return "saved";
    }
    catch{
      return "Not saved"
     }
 }

If you notice, I am using the UserManager that comes with Identity and it brings a variety of methods already implemented that make it easier for you to work with Entities related to it, such as Users, Roles. In essence the first thing is to find the user you want to edit in the BD, hence the cod: ( var user = await _userManager.FindByIdAsync(Id); ), where the Id is passed as a parameter from another view. Also, if you notice, I have eliminated all the parameters that you passed in the action, since passing an ApplicationUser as a parameter and waiting for the attributes of that class, unless you wanted to make a Bind of that class, which would be of the following way:

 public async Task<string> EditUsuario(string Id,[Bind("Id,Email,PhoneNumber,UserName")] ApplicationUser applicationUser)

This way you pass an ApplicationUser but you will not validate all the attributes of the class, but those specified in [Bind("")]

Once the User is found, you pass the values collected in your form and try to save that using _userManager.UpdateAsync(user);

_userManager is declared in the same way that you declared _context at the beginning of your controller and if you take a look at the methods it contains you can see that it helps you in many things related to Identity. Greetings

    
answered by 03.04.2018 / 16:30
source