title: MVC4 ViewData.TemplateInfo.HtmlFieldPrefix generates an extra dot
body: <p>I'm trying to get name of input correct so a collection of objects on my view model can get bound.</p>
  <pre><code>   @{ViewData.TemplateInfo.HtmlFieldPrefix = listName;}
              @Html.EditorFor(m =&gt; m, &quot;DoubleTemplate&quot;, new { 
                  Name = listName,
                  Index = i,
                  Switcher = (YearOfProgram &gt;= i +1)
              })
  </code></pre>
  <p>As you can see here, I pass in the &quot;listName&quot; as the prefix for my template, the value of listName = &quot;MyItems&quot;.</p>
  <p>And here is my template:</p>
  <pre><code>@model Web.Models.ListElement
  
  @if (ViewData[&quot;Switcher&quot;] != null)
  {
      
      var IsVisible = (bool)ViewData[&quot;Switcher&quot;];
      var index = (int)ViewData[&quot;Index&quot;];
      var thisName = (string)ViewData[&quot;Name&quot;] + &quot;[&quot; + index + &quot;].Value&quot;;
      var thisId = (string)ViewData[&quot;Name&quot;] + &quot;_&quot; + index + &quot;__Value&quot;;
      if (IsVisible)
      {
          @*&lt;input type=&quot;text&quot; value=&quot;@Model.Value&quot; name=&quot;@thisName&quot; id =&quot;@thisId&quot; class=&quot;cell@(index + 1)&quot;/&gt;*@
          @Html.TextBoxFor(m =&gt; m.Value, new { @class =&quot;cell&quot; + (index + 1)})
          @Html.ValidationMessageFor(m =&gt; m.Value)
      }
  }
  </code></pre>
  <p>but I found that the generated name becomes this: <strong>MyItems.[0].Value</strong></p>
  <p>It has one extra dot. How can I get rid of it?</p>
  <p>Incidentally, I tried to manually specify the name inside the template and found the name gets overridden by the Html helper.</p>
  <h2>Update</h2>
  <p>The reason why I have to manually set the HtmlFieldPrefix is the property name (MyItems which is a list of objects) will get lost when MyItems is passed from main view to the partial view. By the time, the partial view called my template and passed in one object in MyItems, the template itself has no way to figure out the name of MyItems as it has been lost since the last &quot;pass-in&quot;.</p>
  <p>So that's why I have to manually set the html field prefix name. And I even tried to use something similar to reflection(but not reelection, I forgot the name) to check the name of passed in object and found it returned &quot;Model&quot;.</p>
  <hr />
  <h2>Update 2</h2>
  <p>I tried Stephen's approach, and cannot find the html helper PartialFor().</p>
  <p><img src="https://i.stack.imgur.com/w7ykw.png" alt="enter image description here" /></p>
  <p>I even tried to use this in my main view:</p>
  <pre><code>Html.Partial(Model, &quot;_MyPartialView&quot;);
  </code></pre>
  <p>In Partial View:</p>
  <pre><code>@model MvcApplication1.Models.MyModel
  &lt;h2&gt;My Partial View&lt;/h2&gt;
  
  
  @Html.EditorFor(m =&gt; m.MyProperty)
  </code></pre>
  <p>Here is my templat:</p>
  <pre><code>@model  MvcApplication1.Models.ListElement
  
  @Html.TextBoxFor(m =&gt; m.Value)
  </code></pre>
  <p>Here is my Model:</p>
  <pre><code> public class MyModel
      {
          private List&lt;ListElement&gt; myProperty;
          public List&lt;ListElement&gt; MyProperty
          {
              get
              {
                  if (myProperty == null)
                  {
                      this.myProperty = new List&lt;ListElement&gt;() { new ListElement() { Value = 12 }, new ListElement() { Value = 13 }, new ListElement() { Value = 14 }, new ListElement() { Value = 15 }, };
                  }
                  return this.myProperty;
              }
              set
              {
                  this.myProperty = value;
              }
          }
      }
  
      public class ListElement
      {
          [Range(0, 999)]
          public double Value { get; set; }
      }
  </code></pre>
  <p>And here is my controller:</p>
  <pre><code>public ActionResult MyAction()
  {
      return View(new MyModel());
  }
  </code></pre>
  <p>It only generates raw text(&quot;12131415&quot;) for me, instead of the wanted text box filled in with 12 13 14 15.</p>
  <p>But if I specified the template name, it then throws an exception saying:</p>
  <pre><code>The template view expecting ListElement, and cannot convert
  List&lt;ListElement&gt; into ListElement.
  </code></pre>