title: Razor LabelFor and DropdownFor
body: <p>I am using MVC5 with EF6. I had a requirement for creating a couple of dozen Dropdown Lists in a view that contain user-selectable preferences that are then used throughout the web application.</p>
  
  <p>The problem I am having is an error on both the LabelFor and DropdownListFor lines in the razor View:</p>
  
  <blockquote>
    <p>Compiler Error Message: CS0411: The type arguments for method 'System.Web.Mvc.Html.LabelExtensions.LabelFor System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, string)' cannot be inferred from the usage. Try specifying the type arguments explicitly.</p>
  </blockquote>
  
  <p>I've traced through my code to make sure <code>Model.Preferences</code> contains the data I intended, and it does. Actually, it works fine with <code>Html.Label</code> and <code>Html.DropdownList</code>, but I couldn't get the Selected Item to work that way, so I switched to <code>DropdownListFor</code> since I've found it easier to work with in that respect.</p>
  
  <p>Here are snippets of my code (a work in progress):</p>
  
  <p><strong>A simple class to hold an IEnumerable of SelectListItem:</strong></p>
  
  <pre><code>public class Preference
  {
      public string ControlType;          // "Select", "Textbox"
      public string Label;                // Label of control
      public IEnumerable&lt;SelectListItem&gt; ListItems;
  }
  </code></pre>
  
  <p><strong>MODEL</strong> (An IEnumerable of Preference gives me 'Preferences')</p>
  
  <pre><code>public class UserPreferencesViewModel
  {
      public IEnumerable&lt;Preference&gt; Preferences { get; set; }
  }
  </code></pre>
  
  <p><strong>CONTROLLER</strong></p>
  
  <pre><code>List&lt;Preference&gt; Preferences = new List&lt;Preference&gt;();
  
  foreach (var g in uomGroups)
  {
      List&lt;SelectListItem&gt; listitems = new List&lt;SelectListItem&gt;();
  
      foreach (var w in g.Options)
      {
          ... Omitted for brevity ...
  
          listitems.Add( new SelectListItem { Text = w.UOMId, Value = w.UOMId, Selected = isSelected } )
      }
  
      var p = g.Options.ToArray();
      Preferences.Add( new Preference
      {
          ControlType = "Select",
          Label = g.Preference,
          ListItems = listitems
      });
  }
  
  model.Preferences = Preferences.AsEnumerable();
  
  return View(model);
  </code></pre>
  
  <p><strong>VIEW</strong></p>
  
  <pre><code>&lt;tbody&gt;
  @foreach (Preference preference in Model.Preferences)
  {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.LabelFor(
                  x =&gt; x.Label, 
                  new { @class = "col-md-3 control-label" }
                  )
          &lt;/td&gt;
  
          &lt;td&gt;
              @Html.DropDownListFor(
                  x =&gt; x.Label,
                  new SelectList(preference.ListItems, "Value", "Text"),
                  new { @class = "col-md-3 form-control" }
                  )
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  &lt;/tbody&gt;
  </code></pre>
  
  <p>From the code above, does anyone see where the problem is? I've been trying to figure this out for hours and just don't see it. </p>