Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can the roles for individual users be listed, selected and edited with membership info
    text
    copied!<p>I'm trying to create an admin section to manage users using .net MVC3 in vs 2010. I've figured out how to create and edit new users and roles separately. But I am struggling to figure out how to add roles when I create or edit a new user. This is as far as I've gotten:</p> <p>In my Model:</p> <pre><code>public class UserModel { [Required] [Display(Name = "User name")] public string UserName { get; set; } [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email address")] public string Email { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } } public class IndexViewModel { public IEnumerable&lt;UserModel&gt; Users { get; set; } public IEnumerable&lt;string&gt; Roles { get; set; } } </code></pre> <p>In my Controller</p> <pre><code>public ActionResult Index() { return View( new IndexViewModel { Users = Membership.GetAllUsers().Cast&lt;MembershipUser&gt;().Select(x =&gt; new UserModel { UserName = x.UserName, Email = x.Email, }), Roles = Roles.GetAllRoles() }); } </code></pre> <p>And in the View:</p> <pre><code>@model IEnumerable&lt;BBmvc.Areas.Tools.Models.IndexViewModel&gt; //... @foreach (var item in Model) { foreach (var user in item.Users) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; user.UserName) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; user.Email) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; user.Password) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; user.ConfirmPassword) &lt;/td&gt; &lt;td&gt; @Html.ActionLink("Edit", "Edit", new { /* id=user.PrimaryKey */ }) | @Html.ActionLink("Details", "Details", new { /* id=user.PrimaryKey */ }) | @Html.ActionLink("Delete", "Delete", new { /* id=user.PrimaryKey */ }) &lt;/td&gt; &lt;/tr&gt; } } </code></pre> <p>I am seeing this error: </p> <blockquote> <p>The model item passed into the dictionary is of type 'BBmvc.Areas.Tools.Models.IndexViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable</p> </blockquote> <p>I'm confused. Am I on the right track? Right now I'm simply trying to get the roles to display on the page ... eventually I need them to be filtered for each user so that only the roles a user is in will be listed.</p> <p><strong>EDIT:</strong> The answer below fixed the viewmodel problem I was having. The ultimate solution to displaying roles for each user wasn't that involved. I added a list to the UserModel:</p> <pre><code>public IEnumerable&lt;string&gt; UserRoles { get; set; } </code></pre> <p>Filled it in the Controller:</p> <pre><code> public ActionResult Index2() { var model = Membership.GetAllUsers().Cast&lt;MembershipUser&gt;().Select(x =&gt; new UserModel { UserName = x.UserName, Email = x.Email, UserRoles = Roles.GetRolesForUser(x.UserName) }); return View(model); } </code></pre> <p>And then displayed it in the view:</p> <pre><code>@foreach (var item in Model) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.UserName) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Email) &lt;/td&gt; &lt;td&gt; @foreach (var role in item.UserRoles) { @role } &lt;/td&gt; &lt;td&gt; @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>}</p> <p>Thanks to everyone!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload