title: MVC4 View decision based on number of items in Select List
body: <p>I am creating an MVC4 application using the Razor View engine and I would like to make a decision in my <code>View</code> based on the number of items in a <code>Select List</code>. My Select Lists are created in the controller and saved in the ViewData collection. Basically, if there is only one item available in the select list, I plan to hide the dropdown in the view and hard-code the value using a hidden field.</p>
  
  <p>When I access the "Items" property, Intellisense does not provide a method to count the number of List Items. How do I count the items in the select list from the Razor View?</p>
  
  <p>Index.cshtml</p>
  
  <pre><code>@model Mvc4.Models.PhoneRecord
  ...
      &lt;div class="editor-field"&gt;
          @{
              // I need logic here to count items in the select list.
              // XXXXX is of course a compile error, there is no Intellisense
              // option that provides a "count" method, what should I use?
              if (((SelectList)ViewData["_CellPhoneCarrier"]).Items.XXXXX == 1)) 
              {
                  @Html.Raw("note: value is hardcoded.")
                  @Html.HiddenFor(m =&gt; m.CellPhoneCarrier)
              }
              else
              {
                  @Html.DropDownListFor(m =&gt; m.CellPhoneCarrier, (SelectList)ViewData["_CellPhoneCarrier"])
              }
          }
      &lt;/div&gt;
  ...
  </code></pre>