title: ASP.NET MVC - Form Returns Null Model Unless Model is Wrapped in a Custom ViewModel
body: <p>I have a pair of views in my application that both display the same Editor Template for one of my model items; of the two views ("Add" and "Edit"), "Edit" works fine, but "Add" is returning null for the model when my controller action handles the post.</p>
  
  <p>I found that if I give the "Add" view a custom ViewModel and call <code>Html.EditorFor(p =&gt; p.PageContent)</code> rather than simply calling the EditorFor() on the whole Model object- <code>Html.EditorFor(p =&gt; p)</code>, then the form returns the correct, non-null model, but that generates other problems pertaining to my client-side scripting and control IDs (as now all of the fields are prefixed with "PageContent_"). I am using the same Editor Template technique in a few different places throughout my application and none of the others are exhibiting this odd dependency on a ViewModel. </p>
  
  <p>Has anyone else ever experienced similar problems?</p>
  
  <p><strong>Edit View</strong></p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Areas/Admin/Views/Shared/Site.Master"
  Inherits="System.Web.Mvc.ViewPage&lt;PageContent&gt;" %&gt;
  
  &lt;% using (Html.BeginForm())
     { %&gt;
  &lt;%=Html.Hidden("PageID", Model.Page.ID) %&gt;
  &lt;%=Html.EditorFor(p =&gt; p)%&gt;
  &lt;input type="submit" name="btnSave" value="Save" /&gt;
  &lt;input type="submit" name="btnCancel" value="Cancel" class="cancel" /&gt;
  &lt;% }
  </code></pre>
  
  <p><strong>Action (Working)</strong></p>
  
  <pre><code>[HttpPost, ValidateInput(false)]
  public ActionResult EditContent(int id, FormCollection formCollection) {}
  </code></pre>
  
  <hr>
  
  <p><strong>Add View</strong></p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Areas/Admin/Views/Shared/Site.Master"
  Inherits="System.Web.Mvc.ViewPage&lt;PageContent&gt;" %&gt;
  
  &lt;% using (Html.BeginForm())
     { %&gt;
  &lt;%=Html.Hidden("PageID", ViewData["PageID"]) %&gt;
  &lt;%=Html.EditorFor(p =&gt; p)%&gt;
  &lt;input type="submit" name="btnSave" value="Save" /&gt;
  &lt;input type="submit" name="btnCancel" value="Cancel" class="cancel" /&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p><strong>Action (Failing)</strong></p>
  
  <pre><code>// content is ALWAYS null
  [HttpPost, ValidateInput(false)]
  public ActionResult AddContent(PageContent content, FormCollection formCollection) {}
  </code></pre>
  
  <hr>
  
  <p><strong>Before you cry "duplicate"</strong></p>
  
  <p>This question does relate to <a href="https://stackoverflow.com/questions/1801136/asp-net-mvc-debugging-model-binding-problems">this one</a>, but this question is intended to target the specific problem I am experiencing rather than the more general question asked there.</p>