title: Why does setting the Name attribute explicitly on a Partial View input cause the Value to change after POST?
body: <p>I ran across a problem today where the Code value of a nested object in one of our forms was being changed to an incorrect value. After some digging, I discovered that it is being assigned the value of the Parent object Code, only after POSTing, and only when I try to set the Name attribute explicitly with Html.TextBoxFor's second object parameter.</p>
  
  <p>I setup a simple MVC(Version 5.2.2.0) project to isolate the issue. Here is the code for that.</p>
  
  <p>Models</p>
  
  <pre><code>public class Parent
  {
      public string Code { get; set; }
      public Child Child { get; set; } 
  }
  
  public class Child
  {
      public string Code { get; set; }
  }
  </code></pre>
  
  <p>Controllers</p>
  
  <pre><code>public class ParentController : Controller
  {
      public ActionResult Show()
      {
          var child = new Child() { Code = "999"};
          var parent = new Parent() { Code = "1", Child = child };
  
          return View("Show", parent);
      }
  
      public ActionResult Update(Parent parent)
      {
          return View("Show", parent);
      }
  }
  </code></pre>
  
  <p>Views/Parent/Show</p>
  
  <pre><code>@model TextBoxForBugTest.Models.Parent
  
  @using (Html.BeginForm("Update", "Parent"))
  {
      @Html.TextBoxFor(o =&gt; o.Code)
      @Html.Partial("~/Views/Child/Show.cshtml", Model.Child)
      &lt;button type="submit"&gt;Submit&lt;/button&gt;
  }
  </code></pre>
  
  <p>Views/Child/Show</p>
  
  <pre><code>@model TextBoxForBugTest.Models.Child
  
  @Html.TextBoxFor(o =&gt; o.Code, new { Name = "Child.Code" })
  </code></pre>
  
  <p>When I first load /Parent/Show, I see the correct values in the inputs: 1(Code), and 999(Child.Code). </p>
  
  <p><a href="https://i.stack.imgur.com/zpXJP.png" rel="nofollow noreferrer">Before POST</a></p>
  
  <p>However, after returning from the Update Action Method after submitting the form, Child.Code has been assigned the Value "1" - the Parent Code.</p>
  
  <p><a href="https://i.stack.imgur.com/m0L57.png" rel="nofollow noreferrer">After POST</a></p>
  
  <p>I've found that I can fix the issue by setting the HtmlFieldPrefix.</p>
  
  <pre><code>@model TextBoxForBugTest.Models.Child
  
  @{ Html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "Child"; }
  
  @Html.TextBoxFor(o =&gt; o.Code)
  </code></pre>
  
  <p>or by using a local variable</p>
  
  <pre><code>@model TextBoxForBugTest.Models.Child
  
  @{ var theCode = Model.Code; }
  
  @Html.TextBoxFor(o =&gt; theCode, new { Name = "Child.Code" })
  </code></pre>
  
  <p>but I'd like to understand why. What is going on here? Why is Child.Code being assigned the value of Parent.Code after POSTing?</p>
  
  <p>I also found some related questions that get into using extensions, but they seem to be answering different questions </p>
  
  <p><a href="https://stackoverflow.com/questions/1488890/asp-net-mvc-partial-views-input-name-prefixes?lq=1">ASP.NET MVC partial views: input name prefixes</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4898321/asp-mvc-3-razor-add-model-prefix-in-the-html-partialview-extension?lq=1">ASP.MVC 3 Razor Add Model Prefix in the Html.PartialView extension</a></p>
  
  <p>***Edit - It's clear from the answers that I did a poor job of stating my actual question, so I'll attempt to clarify a bit more here.</p>
  
  <p>The problem I was seeing that was leading to an end user identified bug was that</p>
  
  <pre><code>@Html.TextBoxFor(o =&gt; o.Code, new { Name = "Child.Code" })
  </code></pre>
  
  <p>was generating html with a different "value" the second time it was called(after POSTing).</p>
  
  <p>I was able to solve that problem by setting the Html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix. Stephen Muecke also pointed out another - probably better - solution to that problem in Editor Templates.</p>
  
  <p>What I was trying to ask though was this:</p>
  
  <p>Why does</p>
  
  <pre><code>@Html.TextBoxFor(o =&gt; o.Code, new { Name = "Child.Code" })
  </code></pre>
  
  <p>generate </p>
  
  <pre><code>&lt;input name="Child.Code" id="Code" type="text" value="999"&gt;
  </code></pre>
  
  <p>the first time (/Parent/Show), but then generate</p>
  
  <pre><code>&lt;input name="Child.Code" id="Code" type="text" value="1"&gt;
  </code></pre>
  
  <p>the second time (after POSTing to /Parent/Update)?</p>
  
  <p>The Form Data that gets POSTed is </p>
  
  <p><a href="https://i.stack.imgur.com/r4vQN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r4vQN.png" alt="enter image description here"></a></p>
  
  <p>and the binded Model in </p>
  
  <pre><code>public ActionResult Update(Parent parent)
  {
      return View("Show", parent);
  }
  </code></pre>
  
  <p>has the expected values of Parent.Code == 1 and Child.Code == 999.</p>
  
  <p>I think Stephen Muecke is probably close to the answer I'm looking for in his comment </p>
  
  <blockquote>
    <p>Note also that the new { Name = "Child.Code" } hack does not change the id attribute and you have invalid html. – Stephen Muecke</p>
  </blockquote>
  
  <p>Indeed, using </p>
  
  <pre><code>@Html.TextBoxFor(o =&gt; o.Code, new { Name = "Child.Code" })
  </code></pre>
  
  <p>, I end up with 2 inputs with id="Code", which is invalid according to <a href="https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute" rel="nofollow noreferrer">the spec</a>.</p>
  
  <p>Even knowing that though, I still don't understand why the value attribute generated by TextBoxFor is different based on whether I'm GETing /Parent/Show or POSTing to /Parent/Update.</p>