简体   繁体   中英

RadioButtonFor in a for loop for a model list

Problem is simple, I have a partial view whose model is a list. I am trying to bind radio buttons for a boolean parameter for each item in the list. Only one item in the list can be selected, hence the radio buttons. The model is correctly binding for all other parameters EXCEPT for the boolean that should be bound to the radio button. The following code below is a truncated version of my partial.

NOTE - I need to use the "BeginRouteForm" helper because of multiple routings that my app has. Also I am using a "for" loop instead of a "foreach" cause it seems as if foreach did not properly bound my model when sending it off to the controller.:

@model TestListModel
@using (Ajax.BeginRouteForm("Default", new { action = "Submit", controller = "TheController" }, new AjaxOptions { OnSuccess = "DoSomething" })) {
<table>
    <thead>
        <tr>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < Model.List.Count(); i ++) {
            @Html.HiddenFor(x => Model.List[i].Id)
            <tr>
                <td>
            @{
                Dictionary<string, object> attributes = new Dictionary<string, object>();
                attributes.Add("Name", "test");
                attributes.Add("id", "");
                @Html.RadioButtonFor(x => x.SelectedId, Model.List[i].Id, attributes)
            }
            </tr>
        }
    </tbody>
</table>
    <input type="submit" value="Submit" style="margin: 10px 0px 0px 0px;" />
}

UPDATE: Ok so I am dumb (probably being end of day and all) but I was modeling my radiobuttons as if they were a checkboxlist. Explains why the binding was incorrectly working. I updated the code above to my new code now I am using a model with a "selectedid" instead of an IEnumerable. Problem is, it's still not working

删除以下内容即可解决。

attributes.Add("Name", "test");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM