title: Cannot display SelectList values from Lambda in Razor
body: <p>In MVC.</p>
  
  <p>I am getting this error message when trying to display a dropDownList in Razor.</p>
  
  <blockquote>
    <p>DataBinding: 'System.Int32' does not contain a property with the name 'BranchId'.</p>
  </blockquote>
  
  <p>In the watch window I am able to see 1 record is being returned from the database containing a <code>BranchId</code> and <code>BranchName</code>
  I have no reference to the <code>SelectList</code> in the Model class.</p>
  
  <p>In the Controller class</p>
  
  <pre><code>var list = new SelectList(db.Branchs.Where(branches =&gt; !db.CompanyBranches
      .Any(d =&gt; d.BranchId == branches.BranchId))
      .Select(branches =&gt; branches.BranchId).ToList(), 
      "BranchId", "BranchNumber");
  ViewBag.Branch = new SelectList(list);
  </code></pre>
  
  <p><strong>In the Create.cshtml</strong></p>
  
  <pre><code>@Html.DropDownList("Branch", new SelectList(ViewBag.Branch, "BranchID", "BranchName"), htmlAttributes: new { @class = "form-control" })
  </code></pre>
  
  <p>Thanks Stephen</p>
  
  <p>I changed the lambda expression in my CompanyBranchesController as you suggested
    it is now
    var list = db.Branchs.Where(branches => !db.CompanyBranches
                                       .Any(d => d.BranchId == branches.BranchId))
                                       .Select(b => new { BranchId = b.BranchId,      BranchName = b.BranchName }).ToList();</p>
  
  <p>The Create.cshtml for the dropdownlist is
   @Html.DropDownList("Branch", ViewBag.Branch as IEnumerable,    htmlAttributes: new { @class = "form-control" })</p>
  
  <p>The result in the dropdownlist is
  {BranchId = 5, BranchNumber = Br0003}</p>
  
  <p>I have been playing with it including adding to the CompanyBranchController
   List items = new List();
                  foreach (var i in list)
                  {
                      SelectListItem s = new SelectListItem();
                      s.Text = i.BranchName.ToString();
                      s.Value = i.BranchId.ToString();
                      items.Add(s);<br>
                  }</p>
  
  <p>As well as trying different razor expressions but with no success.</p>
  
  <p>Any Idea where I am going wrong?</p>