简体   繁体   English

用于模型列表的for循环中的RadioButtonFor

[英]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. 注意-由于我的应用具有多个路由,因此我需要使用“ BeginRouteForm”帮助器。 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.: 另外,我使用的是“ for”循环,而不是“ foreach”,因为当将其发送给控制器时,似乎foreach没有正确绑定我的模型。

@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. 我将上面的代码更新为新代码,现在我使用的是带有“ selectedid”而不是IEnumerable的模型。 Problem is, it's still not working 问题是,它仍然无法正常工作

删除以下内容即可解决。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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