TextBoxFor does not create correct name attribute for the input element in ASP.NET MVC5?
<p>I noticed that my viewmodel properties are being posted back as nulls. Upon further investigation, the problem was found to be that the name attributes of textboxes (textboxes generated via TextBoxFor helper methods) were missing the action's viewmodel variable paramter names in them. </p>

<p>Perhaps the example code will make it clearer -</p>

<p><strong>ViewModel</strong></p>

<pre><code>public class ViewModel {
    public string ID {get;set;}
    public List&lt;ViewModelChild&gt; ViewCustomNames{ get; set; }
}
</code></pre>

<p><strong>Controller Action</strong> (ViewModel contains a collection property ViewCustomNames)</p>

<pre><code>public ActionResult Create(ViewModel viewModel1) {...}
</code></pre>

<p><strong>View</strong> (strongly typed with ViewModel)</p>

<pre><code>@Html.TextBoxFor(vc =&gt; vc.ID) //This will not be posted back
&lt;input name="viewModel1.ID" type="text" /&gt; //This will be posted back
@for(int i = 0; I &lt; Model.ViewCustomNames.Count;i++)
    @Html.TextBoxFor(m =&gt; m.ViewCustomNames[i].SomeProperty)
</code></pre>

<p><strong>Generated HTML</strong></p>

<pre><code>&lt;input name="ID"...
&lt;input name="ViewCustomNames[0].SomeProperty" id="ViewCustomNames_0_SomeProperty" type="text" value=""&gt;
</code></pre>

<p><strong><em>What the generated HTML should be</em></strong></p>

<pre><code>&lt;input name="viewModel1.ID"...
&lt;input name="viewModel1.ViewCustomNames[0].SomeProperty" id="doesn't-matter-for-this-example" type="text" value=""&gt;
</code></pre>

<p><strong>Question</strong>
I've fixed it by replacing the TextBoxFor(...) with explicit HTML input elements but I wonder if it's an MVC5 bug or if I'm missing something obvious here. I can't imagine the standard TextBoxFor method that ships with ASP.NET to be not working, it has to be something that I'm doing wrong. Any ideas?</p>

<h2>----------------------------------</h2>

<p>Adding the actual code being used in my project for reference -</p>

<p><strong>Controller*</strong></p>

<pre><code>public class ViewCustomViewModelsController : Controller {
[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ViewCustomId,ViewCustomName,ReconContextId,ViewCustomFieldCollection")] ViewCustomViewModel viewCustomViewModel) { }
</code></pre>

<p>}</p>

<p><strong>View</strong></p>

<pre><code>@model AR.UI.MVC.ViewModels.ViewCustomViewModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

&lt;div class="form-horizontal"&gt;
    &lt;h4&gt;ViewCustomViewModel&lt;/h4&gt;
    &lt;hr /&gt;
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model =&gt; model.ViewCustomId)

    &lt;div class="form-group"&gt;
        &lt;dl class="dl-horizontal"&gt;
            &lt;dt&gt;ViewCustomId&lt;/dt&gt;
            &lt;dd&gt;@*@Html.TextBoxFor(vc =&gt; vc.ViewCustomId)*@ //Posts back null
                &lt;input name="viewCustomViewModel.ViewCustomId" type="text" value="@Model.ViewCustomId" /&gt; //Posts back the correct value
            &lt;/dd&gt;
            &lt;dt&gt;Name&lt;/dt&gt;
            &lt;dd&gt;@Html.TextBoxFor(vc =&gt; vc.ViewCustomName) //Posts back null
            &lt;input name="viewCustomViewModel.ViewCustomName" type="text" value="@Model.ViewCustomName" /&gt; //Posts back the correct value
            &lt;/dd&gt;
            &lt;dt&gt;ReconContext&lt;/dt&gt;
            &lt;dd&gt;@Html.DropDownList("viewCustomViewModel.ReconContextId", ViewBag.ReconContextList as SelectList)&lt;/dd&gt;
        &lt;/dl&gt;
        &lt;span&gt;Field&lt;/span&gt;
        @Html.DropDownList("Field", new SelectList(ViewBag.FieldList, "FieldId", "FieldName"), new { id = "ddlField" })
        &lt;a href="javascript:void(0)" onclick="AddField($('#ddlField').val(), $('#ddlField :selected').text());return false;"&gt;Add&lt;/a&gt;
        &lt;table id="tableSelectedFields"&gt;
            &lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;FieldId&lt;/th&gt;&lt;th&gt;FieldName&lt;/th&gt;&lt;th&gt;DisplayOrder&lt;/th&gt;&lt;th&gt;SortOrder&lt;/th&gt;&lt;th&gt;&lt;/th&gt;&lt;/tr&gt;
            @for (int fieldCount = 0; fieldCount &lt; Model.ViewCustomFieldCollection.Count; fieldCount++)
            {
                &lt;tr&gt;
                    &lt;td&gt;&lt;input name="viewCustomViewModel.ViewCustomFieldCollection.Index" type="text" value="@fieldCount" /&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;input name="viewCustomViewModel.ViewCustomFieldCollection[@fieldCount].ViewCustomFieldId" type="text" value="@Model.ViewCustomFieldCollection[@fieldCount].ViewCustomFieldId" /&gt; &lt;/td&gt;
                    &lt;td&gt;&lt;input name="viewCustomViewModel.ViewCustomFieldCollection[@fieldCount].FieldId" type="text" value="@Model.ViewCustomFieldCollection[@fieldCount].FieldId" /&gt; &lt;/td&gt;
</code></pre>

<p><strong>ViewModel</strong></p>

<pre><code>namespace AR.UI.MVC.ViewModels
{
public class ViewCustomViewModel
{
    [Key]
    public int ViewCustomId { get; set; }
    public string ViewCustomName { get; set; }
    public int ReconContextId { get; set; }
    public int ReconContextName { get; set; }
    public List&lt;ViewCustomFieldViewModel&gt; ViewCustomFieldCollection { get; set; }
    public ViewCustomViewModel() { ViewCustomFieldCollection = new List&lt;ViewCustomFieldViewModel&gt;(); }
    public ViewCustomViewModel(int viewCustomId, string viewCustomName)
    {
        ViewCustomId = viewCustomId;
        ViewCustomName = viewCustomName;
        ViewCustomFieldCollection = new List&lt;ViewCustomFieldViewModel&gt;();
    }
}

public class ViewCustomFieldViewModel
{
    [Key]
    public int ViewCustomFieldId { get; set; }
    public int ViewCustomId { get; set; }
    public int FieldId { get; set; }
    public string FieldName { get; set; }
    public int DisplayOrder { get; set; }
    public int SortOrder { get; set; }
    public ViewCustomFieldViewModel() { }
    public ViewCustomFieldViewModel(int viewCustomFieldId, int viewCustomId, int fieldId, string fieldName, int displayOrder, int sortOrder)
    {
        ViewCustomFieldId = viewCustomFieldId;
        ViewCustomId = viewCustomId;
        FieldId = fieldId;
        FieldName = fieldName;
        DisplayOrder = displayOrder;
        SortOrder = sortOrder;
    }
}
</code></pre>