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>