0

Hi there) I'm a newbie in asp and I'm realizing role based security in my project, using ApplicationUsers class. So i created page, where admin can set roles to users, but i can't set another role to user, because in edit post method ModelState.IsValid value is false, because it can't convert string id value to role. I tried set value to ModelState, but i can't because it hasn't setter. Help, please, how can i fix it by another way? My code below...

    public ActionResult Edit(string id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        ApplicationUser applicationUser = db.Users.Find(id);
        if (applicationUser == null)
        {
            return HttpNotFound();
        }
        var roles = db.Roles.Select(x => new SelectListItem { Text = x.Name, Value = x.Id }).ToArray();
        ViewBag.Roles = roles;
        return View(applicationUser);
    }

    // POST: ApplicationUsers/Edit/5
    // Чтобы защититься от атак чрезмерной передачи данных, включите определенные свойства, для которых следует установить привязку. Дополнительные 
    // сведения см. в статье http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,Email,EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName,Roles")] ApplicationUser applicationUser)
    {
        if (ModelState.IsValid)
        {
            db.Entry(applicationUser).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(applicationUser);
    }

and view:

    <div class="form-group">
        @Html.LabelFor(model => model.Roles, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(x => x.Roles, (IEnumerable<SelectListItem>)ViewBag.Roles, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Roles, "", new { @class = "text-danger" })
        </div>
    </div>
  • From your code it looks like you are posting the User class (from Identity Framework). I wouldn't recommend this. Use ViewModels instead and don't send critical fields. I don't want clients to be able to update the PasswordHash. In your viewmodel you can add a roleId collection with just the roleid's. In your Edit method you can update your Identity model. –  Jun 11 '16 at 12:27

1 Answers1

0

Try to set a breaking point just after the if (ModelState.IsValid) in the opening { to see really what is missing the problem is with another propriety that you haven't set? may be there is one required propriety in the ApplicationUser object that is not set or the datatype is not correct.

Tchaps
  • 865
  • 7
  • 21
  • How to get all errors from Asp.Net mvc modelstate: http://stackoverflow.com/questions/1352948/how-to-get-all-errors-from-asp-net-mvc-modelstate –  Jun 11 '16 at 12:40