Wrong Id being passed to userManager from View

Viewed 48

I'm trying to change the role of a user. The 'CurrentRole' does not give an exception when I load the users from the database, only when I try to save the 'UpdateRole' form

User class

public class User : IdentityUser
    {
        private ApplicationUserManager userManager;

        [Required]
        [Display(Name = "Name")]
        public string FullName { get; set; }
        [Required]
        [Display(Name = "Is Suspended")]
        public bool IsSuspended { get; set; }

        //the CurrentRole property is not mapped as a field in the Users table
        //i neeed it to get the current role that the user is logged in

        [NotMapped]
        public string CurrentRole
        {
            get
            {
                if (userManager == null)
                {
                    userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
                }

                return userManager.GetRoles(Id).Single();
            }
            set
            {
                CurrentRole = value;
            }
        }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> 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;
        }
    }

UpdateRole


@{
    ViewBag.Title = "UpdateRole";
}

<h2>UpdateRole</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Change Role</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Id)

    <div class="form-group">
        @Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CurrentRole, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.CurrentRole, new List<SelectListItem> {
                           new SelectListItem { Value = "Administrator" , Text = "Administrator" },
                           new SelectListItem { Value = "Staff" , Text = "Staff" },
                           new SelectListItem { Value = "Customer" , Text = "Customer" }
                        },
                      new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CurrentRole, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back", "ViewAllUsers")
</div>

When I submit the form, it passes a string id into this method

return userManager.GetRoles(Id).Single();

However the stringId isn't any of my users id?

When I comment out the NotMapped currentRole it hits the setter but then obviously fails because the value = null;

[HttpGet]
        public ActionResult UpdateRole(string id)
        {
            User user = db.Users.Find(id);
            return View(user);
        }

        [HttpPost]
        public ActionResult UpdateRole(User model)
        {
            User user = db.Users.Find(model.Id);
            user.CurrentRole = model.CurrentRole;
            db.SaveChanges();
            return RedirectToAction("ViewAllUsers");
        }
0 Answers
Related