Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Updating data in User Accounts - Value cannot be null. Parameter name: manager

$
0
0

I'm having issues with geting the user data to update in MVC5. I created a form and when I click on submit, it runs the method below, which throws the error  

Value cannot be null. Parameter name: manager


I know if has to do with UserManager, but I can't find any resource that helps me with this particular issue. I've been pulling my hair out for a couple days now trying to get the user data to update via the form so any help is greatly appreciated.

Let me know if any other info is needed

Controller

[ValidateAntiForgeryToken]
public async Task<ActionResult> Update(TierViewModel model)
{

ApplicationUser user = UserManager.FindById(User.Identity.GetUserId());


user.FirstName = model.ApplicationUser.FirstName;
user.LastName = model.ApplicationUser.LastName;
user.Email = model.ApplicationUser.Email;
user.Opid = model.ApplicationUser.Opid;
user.PhoneNumbers = model.ApplicationUser.PhoneNumbers;
user.TierId = model.ApplicationUser.TierId;
IdentityResult result = await UserManager.UpdateAsync(user);

return RedirectToAction("EmployeeDetails");


}

ApplicationUser

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace NewProject.Models
{
    public class ApplicationUser : IdentityUser
    {



        [Required(ErrorMessage = "Your must provide a PhoneNumber")]
        [Display(Name = "Home Phone")]
        [DataType(DataType.PhoneNumber)]
        [DisplayFormat(DataFormatString = "{0:###-###-####}")]

        [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid Phone number")]
        public string PhoneNumbers { get; set; }


        public string EmailAccount => Email;
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Tier Tier { get; set; }
        [Display(Name="Tier Level")]
        public int TierId { get; set; }

        public string Opid { 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

            userIdentity.AddClaim(new Claim("PhoneNumbers", this.PhoneNumbers.ToString()));
            userIdentity.AddClaim(new Claim("FirstName", this.FirstName.ToString()));
            userIdentity.AddClaim(new Claim("Opid", this.Opid.ToString()));
            userIdentity.AddClaim(new Claim("TierId", this.TierId.ToString()));



            return userIdentity;
        }


        public ApplicationUser()
        {
            this.TierId = 4;

        }

    }
}           

I also tried using a viewmodel with the update method code, but got a different error on the following line in my .cshtml file saying 

<td>@Html.TextBoxFor(m => m.ApplicationUser.FirstName)</td> 

Error: Object refence not set to an instance of an object

[ValidateAntiForgeryToken]
public async Task<ActionResult> Update(ApplicationUser applicationUser)
{
try
{

var viewModel = new TierViewModel()
{
ApplicationUser = applicationUser,
TierLevel = _context.Tier.ToList()


};

ApplicationUser user = UserManager.FindById(User.Identity.GetUserId());


user.FirstName = viewModel.ApplicationUser.FirstName;
user.LastName = viewModel.ApplicationUser.LastName;
user.Email = viewModel.ApplicationUser.Email;
user.Opid = viewModel.ApplicationUser.Opid;
user.PhoneNumbers = viewModel.ApplicationUser.PhoneNumbers;
user.TierId = viewModel.ApplicationUser.TierId;
IdentityResult result = await UserManager.UpdateAsync(user);
return View("EmployeeDetails", viewModel);

}

catch (ArgumentNullException e)
{
Console.WriteLine(e);

}
return View("EmployeeDetails");


Viewing all articles
Browse latest Browse all 4737

Trending Articles