1

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?

JamesEggers
  • 12,885
  • 14
  • 59
  • 86

2 Answers2

2

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

Community
  • 1
  • 1
epzee
  • 568
  • 8
  • 22
0

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>
apostolov
  • 1,646
  • 16
  • 20