在我网站的某个页面上,某人可以更改现有用户的用户级别。
下拉列表的数据来自名为AspNetRoles的表。 所选数据应保存到AspNetUserRoles
但由于某种原因,我得到一个NULL exeption错误 代码:
控制器:
public ActionResult UserEdit(string Id)
{
var SelectedRoles = from r in db.AspNetRoles
where r.Id == r.Id
orderby r.Id
select new { r.Id, Value = r.Name };
ViewBag.SelectedRoles = new SelectList(SelectedRoles.Distinct(), "Id", "Value");
if (Id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
AspNetUserRoles aspNetUser = db.AspNetUserRoles.Find(Id);
if (aspNetUser == null)
{
return HttpNotFound();
}
return View(aspNetUser);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserEdit([Bind(Include = "UserId,RoleId")] AspNetUserRoles aspNetUserRoles, FormCollection formCollection)
{
aspNetUserRoles.RoleId = (formCollection["SelectedRoles"]);
//ManageMessageId? message;
if (ModelState.IsValid)
{
db.Entry(aspNetUserRoles).State = EntityState.Modified;
db.SaveChanges();
TempData["AlertMessage"] = "User Edited Successfully";
return RedirectToAction("ManageAccounts");
}
return View(aspNetUserRoles);
}
查看:
<div class="form-horizontal">
<h4>AspNetUserRoles</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.UserId)
@Html.HiddenFor(model => model.RoleId)
<div class="form-group">
@Html.Label("Email", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AspNetUsers.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AspNetUsers.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Email bevestigd", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.CheckBoxFor(model => model.AspNetUsers.EmailConfirmed, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AspNetUsers.EmailConfirmed, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Rechten", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m =>m.RoleId, (IEnumerable<SelectListItem>)ViewBag.SelectedRoles, new { @class = "form-control" })
@* @Html.DropDownList("SelectedRoles", null, new { @class = "form-control" })*@
@Html.ValidationMessageFor(model => model.AspNetRoles.Name, "", 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-primary" />
</div>
</div>
</div>
<div>
<button onclick="location.href='@Url.Action("ManageAccounts", "Manage")';return false; " class="btn btn-primary">Back to list</button>
</div>
}
我希望你能帮助我。 如果需要更多信息,我很乐意分享这些信息
答案 0 :(得分:0)
在Controller中尝试:
var SelectedRoles = from r in db.AspNetRoles
where r.Id == r.Id
orderby r.Id
select new { r.Id, r.Name };
ViewBag.SelectedRoles = new SelectList(SelectedRoles, "Id", "Name");
在视图中:
@Html.DropDownListFor(model => model.RoleId, ViewBag.SelectedRoles as SelectListItem))