title: Populating a razor dropdownlist from a List<object> in MVC
body: <p>I have a model:</p>
  
  <pre><code>public class DbUserRole
      {
          public int UserRoleId { get; set; }
          public string UserRole { get; set; }
      }
  
  public class DbUserRoles
      {
          public List&lt;DbUserRole&gt; GetRoles()
          {
              BugnetReports RoleDropDown = new BugnetReports();
              List&lt;DbUserRole&gt; Roles = new List&lt;DbUserRole&gt;();
              DataSet table = RoleDropDown.userRoleDropDown();
              foreach (DataRow item in table.Tables[0].Rows)
              {
                  DbUserRole ur = new DbUserRole();
                  ur.UserRole = Convert.ToString(item["UserRoleName"]);
                  ur.UserRoleId = Convert.ToInt32(item["UserRoleID"]);
                  Roles.Add(ur);
              }
              return Roles;
          }
      }
  </code></pre>
  
  <p>And here is the Controller that loads the view:</p>
  
  <pre><code>        //
          // GET: /Admin/AddNewUser
  
          public ActionResult AddNewUser()
          {
              DbUserRoles Roles = new DbUserRoles();
              return View(Roles.GetRoles());
          }
  </code></pre>
  
  <p>I can get the items in the list to display using a <code>@foreach</code> loop as shown below:</p>
  
  <pre><code>@foreach (var item in Model)
         {
             &lt;tr&gt;
                 &lt;td&gt;
                     @item.UserRoleId
                 &lt;/td&gt;
                 &lt;td&gt;
                     @item.UserRole
                 &lt;/td&gt;
             &lt;/tr&gt;
         }
  </code></pre>
  
  <p>But how do I populate a dropdownlist with the model that is passed through, I have tried </p>
  
  <p><code>@Html.DropDownListFor(x =&gt; x.UserRole)</code></p>
  
  <p>but I'm having no luck.</p>