Strange behaviour with MVC posting list

2019-08-17 06:13发布

问题:

I have the following dynamically created textboxes in my view:

 @for (int i = 0; i < Model.MullionList.Count; i++)
{
    ItemDrops curMullItem = Model.MullionList.ElementAt(i);
    <div class="form-group">
        @Html.Label(curMullItem.ItemName.ToString(), new { @class = "col-sm-6 control-label" })

        <div class="col-sm-6">
            @Html.TextBoxFor(x => x.MullionList[i].ItemPossInfo, new { @class = "form-control" })
            @Html.HiddenFor(x => x.MullionList[i].ItemName)
        </div>
    </div>                                
}

I noticed occaionally for certain types of product this was returning null when posted (for MullionList), even though i selected these dropdowns.

so it works for certain product but not others.

The even weirder part is when i remove this section further down the view it works (ie MulionList is not null when the form is posted)

 <div class="form-group">
    @Html.Label("Glass", new { @class = "col-sm-6 control-label", id = "glass-first-label" })
    <div class="col-sm-6">
        @Html.DropDownListFor(gu => gu.GlassItems[0].Value, Model.GlassTypes, "-- Select --", new { @class = "form-control glass-multi-select", id = "Glass" + 0 })
    </div>
</div>

<div id="hidden-glass-select" style="display: none">
    @for (int i = 1; i < Model.GlassUnitsCount; i++)
    {
        var glassUnit = Model.GlassUnits.ElementAt(i);
        <div class="form-group">
            @Html.Label(glassUnit.ToString(), new { @class = "col-sm-6 control-label" })
            <div class="col-sm-6">
                @Html.DropDownListFor(gu => gu.GlassItems[i].Value, Model.GlassTypes, "-- Select --", new { @class = "form-control glass-multi-select", id = "Glass" + i })
            </div>
        </div>

    }
</div>

<div class="form-group">
    @Html.LabelFor(go => go.GlazzingInfoVal, "Glazzing Option", new { @class = "col-sm-6 control-label" })
    <div class="col-sm-6">
        @Html.DropDownListFor(go => go.GlazzingInfoVal, Model.GlazzingOptions, "-- Select --", new { @class = "form-control" })
    </div>
</div> 

回答1:

I think I have fixed this now.

I renamed all items (lists that started with Glass or Glazzing and it works!)

This looks like a proper MVC bug

Thanks to this answer:https://stackoverflow.com/a/16113919/66975

This has costed me several hours, GRRRR