title: MVC 3 - display value from dictionary in view
body: <p>I suspect this is simple and I'm just missing it.</p>
  
  <p>I have a view model that has a dictionary as one of its properties.  This dictionary is for populating an HTML select with the Category when I'm editing a record.  There is also a property to indicate the selected CategoryID.  However, when I'm just displaying the record I want to display the value of the key/value pair that corresponds with the CategoryID.</p>
  
  <p>My view model:</p>
  
  <pre><code>public class ItemEditVM
  {
      public int ItemID { get; set; }
      public int? CategoryID { get; set; }
      public string ItemDesc { get; set; }
      public Dictionary&lt;int, string&gt; CategorySelect { get; set; }
  }
  </code></pre>
  
  <p>I've tried something like this but I've got the function parameters wrong:</p>
  
  <pre><code>@Html.DisplayFor(model =&gt; model.CategorySelect.TryGetValue(model.CategoryID, out val))
  </code></pre>
  
  <p>I've also tried this which fails with no specific error message:</p>
  
  <pre><code>@Html.DisplayFor(model =&gt; model.CategorySelect.SingleOrDefault(c=&gt; int.Parse(c.Value) == model.CategoryID).Value)
  </code></pre>
  
  <p>How can I do this?</p>
  
  <p>Many thanks</p>
  
  <p>THE CORRECT ANSWER (Thanks to Mystere Man)</p>
  
  <pre><code>@Html.DisplayFor(model=&gt;model.CategorySelect[Model.CategoryID.Value])
  </code></pre>