Reputation: 12935
Looking for some guidance on this issue (below is a parallel example):
I have a view in which I need to display a list of basic user information (e.g. First Name/Last Name) per row. In addition, I need to provide a list of check boxes for that represent permission levels for each user. The list of possible permissions is the same for everyone; however, needs to be dynamically driven by the database since different permission sets may be added or removed.
So...I have a list of users, and a common yet variable list of permissions that needs to be tracked per user.
What's the best way to manage this from an object bind standpoint in a ASP.Net view?
Upvotes: 1
Views: 233
Reputation: 568
I've solved a similar scenario using EditorTemplates
You could have a Users collection in the AssignPermissionViewModel, and a Permission collection in each user (UserModel). Then in your AssignPermission view, you should render the users editor:
@Html.EditorFor(model => model.Users) //here model is a AssignPermissionViewModel
And inside the user editor template you should render the permissions template:
@Html.EditorFor(model => model.Permissions) //here model is a UserModel
This way you avoid having foreach loops all over your AssignPermission view, and you could re-use any editor in another view... (although these are very specific)
NOTE: It would be more appropriate to use a display template instead of an editor template to display the users information, since you're not going to edit any user in this view, check this out: ASP.NET MVC 3 - Partial vs Display Template vs Editor Template
Upvotes: 2
Reputation: 1646
You should create a List of Users in your View Model. A User should be a Class in which you create a property that is a list of Permission Levels, so in your View you sould call something like this:
<ul>
foreach(var user in Model.Users)
{
<li> user.FirstName...
<ul>
foreach(var permissions in user.Permissions)
{
<li> permission.SomeProperty </li>
}
</ul>
</li>
}
</ul>
Upvotes: 0