I just switched my entire application over to using MVC membership using LDAP that my supervisor wrote. Logging in and authenticating works. However, now I need to change the selectLists in my application to pull roles in the membership tables and also pull users in the Technician role. My first crack at the pulling all roles includes this attempt:
in my controller:
ViewBag.TypeId = new SelectList(Roles.GetAllRoles(), "Id", "Name");
and then the code in my razor view:
<div class="form-group">
@Html.Label("Type", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
@Html.DropDownList("TypeId", null, "Select Type", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TypeId, "", new { @class = "text-danger" })
</div>
</div>
I found this but I cannot see how to save the ID in the list is captured or how to convert it to a dropdownlist as opposed to a ListBox:
ASP.net MVC SelectList filed with all roles
Here is the error I receive on the dropdownlist in my view:
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
Additional information: DataBinding: 'System.String' does not contain a property with the name 'RoleId'.
and here is the table:
ViewBag.TypeId = new SelectList(Roles.GetAllRoles(), "RoleId", "RoleName");
您可以获取如下角色的SelectList
。
@Html.DropDownListFor(m => m.TypeId, (SelectList)ViewBag.TypeId, "Select Type", new { @class = "form-control" })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.