title: Proper method of populating a dropdown in ASP.NET MVC
body: <p>This is how I've implemented my dropdown from my model. I just want to check if this is the proper way to do it or is there an easier method? It seems quite convoluted.</p>
  
  <p>The model of my list:</p>
  
  <pre><code>public class ApplicationRolesDropdownListDetailViewModel
  {
      public String RoleId { get; set; }
      public String ApplicationRoleName { get; set; }
  }
  </code></pre>
  
  <p>The viewmodel that goes to my view</p>
  
  <pre><code>public class ApplicationRolesDropdownListViewModel
  {
      public SelectList Roles { get; set; }
  }
  </code></pre>
  
  <p>The controller that gets the list of items and puts it in a dropdown in a view:</p>
  
  <pre><code>    public ActionResult NewRole()
      {
          var applicationRoles = applicationRolesData.GetAllApplicationRoles();
          ApplicationRolesDropdownListViewModel ardlvm = new ApplicationRolesDropdownListViewModel();
          ardlvm.Roles = new SelectList(applicationRoles, "RoleId", "ApplicationRoleName");
          return View("~/Views/Users/Modals/AddRole.cshtml", ardlvm);
      }
  </code></pre>
  
  <p>My View:</p>
  
  <pre><code>    &lt;div class="form-group"&gt;
          @Html.DropDownListFor(m =&gt; m.Roles,Model.Roles, new { @id = "role", @class = "dropdown" })
      &lt;/div&gt;
  </code></pre>
  
  <p>Also, I can't seem to get the RoleId when I select an item from the list via javascript.</p>
  
  <p>EDIT: Added my GetApplicationRoles</p>
  
  <pre><code>    public List&lt;ApplicationRolesDropdownListViewModel &gt; GetAllApplicationRoles()
      {
          List&lt;ApplicationRolesDropdownListViewModel &gt; data = new List&lt;ApplicationRolesDropdownListViewModel &gt;();
          try
          {
              var applicationRoles = dbContext.AspNetRolesExtendedDetails.ToList();
              data = (from ar in applicationRoles
                      join a in dbContext.AspNetApplications
                       on ar.ApplicationId equals a.Id
                      select new ApplicationRolesDropdownListViewModel 
                      {
                          RoleId = ar.Id,
                          ApplicationRoleName = ar.Name + " ( " + a.Name + " )"
                      }).ToList();
          }
          catch (Exception e)
          {
              logger.Error(e, AspNetEventLogs.NotFound);
          }
          return data;
      }
  </code></pre>