title: Checking for Dictionary key from Razor in ASP.NET MVC
body: <p>I am working on an ASP.NET MVC app that I've inherited. The app uses a <code>Dictionary&lt;string, string&gt;</code> for <code>Translations</code>. That <code>Dictionary</code> is stored on the <code>Application</code> object. Currently, I have the following:</p>
  
  <pre><code>@foreach (var key in Model.MyDictionary.Keys)
  {
    var definition = Model.MyDictionary[key];
    ViewDataDictionary viewData = new ViewDataDictionary();
    viewData["key"] = key;
    viewData["value"] = ((Dictionary&lt;string, string&gt;)(HttpContext.Current.Application["Translations"]))[key];
    Html.RenderPartial("~/Views/Shared/_Definition.cshtml", definition, viewData);
  }
  </code></pre>
  
  <p>This approach works except if the <code>key</code> is not found in <code>Translations</code>. How can I elegantly ensure that the key exists in the <code>Translations</code> dictionary from my Razor code? I can't seem to identify a good way to do this.</p>
  
  <p>Thank you for your help and happy holidays.</p>