How can the roles for individual users be listed, selected and edited with membership info?

As you are returning just 1 object of type IndexViewModel you need to change your model to model BBmvc.Areas.Tools.Models. IndexViewModel .

Up vote 0 down vote favorite share g+ share fb share tw.

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: In my Model: 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 Users { get; set; } public IEnumerable Roles { get; set; } } In my Controller public ActionResult Index() { return View( new IndexViewModel { Users = Membership.GetAllUsers().Cast().

Select(x => new UserModel { UserName = x. UserName, Email = x. Email, }), Roles = Roles.GetAllRoles() }); } And in the View: @model IEnumerable //... @foreach (var item in Model) { foreach (var user in item.

Users) { @Html. DisplayFor(modelItem => user. UserName) @Html.

DisplayFor(modelItem => user. Email) @Html. DisplayFor(modelItem => user.

Password) @Html. DisplayFor(modelItem => user. ConfirmPassword) @Html.

ActionLink("Edit", "Edit", new { /* id=user. PrimaryKey */ }) | @Html. ActionLink("Details", "Details", new { /* id=user.

PrimaryKey */ }) | @Html. ActionLink("Delete", "Delete", new { /* id=user. PrimaryKey */ }) } } I am seeing this error: 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 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. EDIT: 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: public IEnumerable UserRoles { get; set; } Filled it in the Controller: public ActionResult Index2() { var model = Membership.GetAllUsers().Cast(). Select(x => new UserModel { UserName = x. UserName, Email = x.

Email, UserRoles = Roles. GetRolesForUser(x. UserName) }); return View(model); } And then displayed it in the view: @foreach (var item in Model) { @Html.

DisplayFor(modelItem => item. UserName) @Html. DisplayFor(modelItem => item.

Email) @foreach (var role in item. UserRoles) { @role } @Html. ActionLink("Edit", "Edit", new { /* id=item.

PrimaryKey */ }) | @Html. ActionLink("Details", "Details", new { /* id=item. PrimaryKey */ }) | @Html.

ActionLink("Delete", "Delete", new { /* id=item. PrimaryKey */ }) } Thanks to everyone! Asp.net-mvc-3 link|improve this question edited Dec 16 '11 at 14:04 asked Dec 15 '11 at 21:18Rob574 60% accept rate.

– DaveShaw Dec 15 '11 at 22:03 @model IEnumerable – Rob Dec 15 '11 at 22:55.

As you are returning just 1 object of type IndexViewModel you need to change your @model to @model BBmvc.Areas.Tools.Models.IndexViewModel. As this single object is holding a list of users and roles... you need to change the view: @model BBmvc.Areas.Tools.Models. IndexViewModel //... @foreach (var user in Model.

Users) { @Html. DisplayFor(modelItem => user. UserName) @Html.

DisplayFor(modelItem => user. Email) @Html. DisplayFor(modelItem => user.

Password) @foreach (var role in Model.Roles. Where(x => x. IdUser == user.

IdUser)) { @role.Name | } @Html. ActionLink("ViewRoles", "View Roles", new { id=user. PrimaryKey }) | @Html.

ActionLink("Edit", "Edit", new { id=user. PrimaryKey }) | @Html. ActionLink("Details", "Details", new { id=user.

PrimaryKey }) | @Html. ActionLink("Delete", "Delete", new { id=user. PrimaryKey }) } This way, you can have the roles of the user, and clicking on "View Roles" or "Setup Roles" (use the link text that best suite your problem) you can have a list of the roles of that user.

There you can add/edit/delete roles for the selected user.

That is much better. How do I get from here to displaying the roles specific to each user? – Rob Dec 16 '11 at 12:14 @Rob, you can have a View to display Roles, and place another link in this one: '@Html.

ActionLink("ViewRoles", "View Roles", new { id=user. PrimaryKey })'. Besides that, you can have a Roles summary in other '' of this table.

See update. – Romias Dec 16 '11 at 12:51 I actually added the list of roles to the UserModel: – Rob Dec 16 '11 at 13:35.

You need to change the @model From: @model IEnumerable To: @model BBmvc.Areas.Tools.Models.IndexViewModel. The code in your controller is just returning an instance of IndexViewModel, not an IEnumerable (in other words a collection/list/array/whatever) of IndexViewModel's.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions