title: DropDownListFor Composite Key with Value Column
body: <p>I'm building a Roles/Permissions component for my application. My Role view model looks like this:</p>
  
  <pre><code>public class RoleView
  {
      public int Id { get; set; }
  
      public ApplicationWithPermissionsView Application { get; set; }
  
      public List&lt;RolePermissionView&gt; Permissions { get; set; }
  
      public IEnumerable&lt;KeyValuePair&lt;string, int&gt;&gt; PermissionValues
      {
          get
          {
              return EnumHelper.ToList&lt;PermissionValue&gt;();
          }
      }
  
      [Required]
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>The RolePermission view model looks like this:</p>
  
  <pre><code>public class RolePermissionView
  {
      public int PermissionId { get; set; }
  
      public int RoleId { get; set; }
  
      public PermissionValue Value { get; set; }
  }
  </code></pre>
  
  <p>PermissionValue is an enum that has members NotSet, Allow, Deny, and stores an int value in the database.</p>
  
  <p>If I go to a role edit page, I display a list of permission categories, which each have a list of permissions, with the respective values (RolePermission) for each permission for that role. As you can see, RolePermissionView has composite keys (PermissionId, RoleId). </p>
  
  <p>How can I build a drop down list, that shows a list of possible permission values and selects the correct permission value for that particular permission?</p>
  
  <p>The following generates the drop down lists, but doesn't select any values. Also, doesn't give each dropdown list a unique name or ID, so binding definitely won't work. I assume if I later move this into an Editor Template they will have uniques.</p>
  
  <pre><code> @foreach (var permission in category.Permissions)
   {             
   &lt;tr&gt;                        
       &lt;td&gt;@permission.Description&lt;/td&gt;                        
       &lt;td&gt;@Html.DropDownListFor(m =&gt; m.Permissions.Find(p =&gt; p.PermissionId == permission.Id).PermissionValue, new SelectList(Model.PermissionValues, "Value", "Key"))&lt;/td&gt;
   &lt;/tr&gt;               
   }
  </code></pre>