简体   繁体   中英

Change ASP.NET Identity register user to allow multiple roles

Right now my code uses the default ASP.NET code to register a user. It displays the list of roles on the view using the following code

    <div class="form-group">
         @Html.LabelFor(model => model.Role, "Role", new {@class = "col-md-2 control-label"})
         <div class="col-md-10">
             @Html.DropDownListFor(model => model.Role, Model.Roles, "--Select Role --")
         </div>
     </div>

I want to allow the person that is creating the account to select multiple roles. Any suggestion on how to change this to a different solution? I was thinking check boxes would be the most ideal.

You could use check boxes; but then you need to draw a checkbox and label for every role in your application. If you ever add roles depending on how you drew them there may be an update required there.

I would choose to use a mutliselect widget/input/dropdown; there are many available from places like kendo core and jqueryUI. You could bind the population of this widget to an Ajax method, Api call, viewModel binding, or viewbag binding. Then update the save method to go through the collection of roles to add the user to that you received from the multiselect. You can bind the results to an IEnumerable with the kendo one automatically.

Additionally you need to check the roles they are currently in and remove any that the user no longer has checked, and the identity/role methods will have issues if you are already in the role its trying to add you to.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM