title: Shorthand for creating a ViewDataDictionary with both a model and ViewData items?
body: <p>Is there any way to create a <code>ViewDataDictionary</code> with a model and additional properties with a single line of code. I am trying to make a <code>RenderPartial</code> call to a strongly-typed view while assembling both the model and some extra display configuration properties without explicitly assembling the ViewDataDictionary across multiple lines. It seems like it would be possible given the <code>RenderPartial</code> overload taking both a model <code>object</code> and a <code>ViewDataDictionary</code> but it looks like it simply ignores the <code>ViewDataDictionary</code> whenever they are both populated.</p>
  
  <pre><code>// FAIL: This will result in ViewData being a ViewDataDictionary
  // where Model = MyModelObject and there are no other parameters available.
  this.Html.RenderPartial("SomePartialView", MyModelObject, new ViewDataDictionary(new { SomeDisplayParameter = true }));
  </code></pre>
  
  <p>I found someone else with the <a href="https://stackoverflow.com/questions/495351/asp-net-mvc-rc1-renderpartial-viewdatadictionary">same problem</a>, but their solution is the same multi-line concept I found: create a discrete <code>ViewDataDictionary</code> with the model, add the new parameter(s) and use it in the <code>RenderPartial</code> call.</p>
  
  <pre><code>var SomeViewData = new ViewDataDictionary(MyModelObject);
  SomeViewData.Add("SomeDisplayParameter", true);
  this.Html.RenderPartial("SomePartialView", SomeViewData);
  </code></pre>
  
  <p>I can always wrap that logic into a <code>ChainedAdd</code> method that returns a duplicate dictionary with the new element added but it just seems like I am missing some way of creating a <code>ViewDataDictionary</code> that would do this for me (and that is a bit more overhead than I was hoping for).</p>
  
  <pre><code>this.Html.RenderPartial("SomePartialView", new ViewDataDictionary(MyModelObject).ChainedAdd("SomeDisplayParameter", true));
  
  public static ViewDataDictionaryExtensions {
      public static ViewDataDictionary ChainedAdd(this ViewDataDictionary source, string key, object value) {
          return source.ChainedAdd(new KeyValuePair&lt;string,object&gt;(key, value));
      }
      public static ViewDataDictionary ChainedAdd(this ViewDataDictionary source, KeyValuePair&lt;string, object&gt; keyAndValue) {
          ViewDataDictionary NewDictionary = new ViewDataDictionary(source);
          NewDictionary.Add(keyAndValue);
          return NewDictionary;
      }
  }
  </code></pre>
  
  <p>As well, trying to assemble a <code>ViewDataDictionary</code> with an explicit <code>Model</code> and <code>ModelState</code> simply causes a compilation error because the ModelState is read-only.</p>
  
  <pre><code>// FAIL: Compilation error
  this.Html.RenderPartial("SomePartialView", new ViewDataDictionary { Model = MyModelObject, ModelState = new ViewDataDictionary( new { SomeDisplayParameter = true }});
  </code></pre>
  
  <p><strong>ANSWER(S):</strong> It looks like Craig and I ended up finding two separate syntaxes that will get the job done. I am definitely biased in this case, but I like the idea of setting the model first and "decorating" it afterwards.</p>
  
  <pre><code>new ViewDataDictionary(MyModelObject) { { "SomeDisplayParameter", true }, { "SomeOtherParameter", 3 }, { "SomeThirdParameter", "red" } };
  
  new ViewDataDictionary(new ViewDataDictionary() { {"SomeDisplayParameter", true }})
      { Model = MyModelObject };
  </code></pre>
  
  <p>Of course, I would still be spinning my wheels without his [eventually spot-on] answer, so, circle gets the square.</p>