Strange behaviour with MVC posting list

痞子三分冷 提交于 2020-01-06 08:28:06

问题


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



来源:https://stackoverflow.com/questions/22982096/strange-behaviour-with-mvc-posting-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!