开发者

Templated list with radio buttons

开发者 https://www.devze.com 2023-04-08 23:52 出处:网络
I have the following code: @using (Html.BeginForm()) { for (int i = 0; i < Model.Themes.Count; i++) { <div class=\"theme\">

I have the following code:

            @using (Html.BeginForm())
                {
                    for (int i = 0; i < Model.Themes.Count; i++)
                    {

                    <div class="theme">
                        <img src="/Content/img/placeholder/@Model.Themes[i].PreviewImg" class="theme-image" />
                        <div class="theme-meta">
                            @Html.RadioButton("Themes", Model.Themes[i].Selected, new { @id = "Themes_" + i + "__Selected" })

                            @Html.TextBoxFor(x=>x.Themes[i].Id)
                            <label>@Model.Themes[i].Name</label>
                        </div>
                    </div>
                    }
                    <div class="submit">
                        <input type="submit" class="dash-module-alt-button submit" name="SelectTheme" />
                    </div>
                }

However submitting this with one of the radio buttons selected does not bind with the selected property set to true.

Using Html.RadioButtonFor means that they a开发者_开发知识库ll have a generated array name so they can all be selected.

HOW the eff am i supposed to do this?

cheers

w://


Not sure if this is what you want but Html.RadioButtonFor will take a value argument -

@Html.RadioButtonFor(x => x.Themes, "blue")
@Html.RadioButtonFor(x => x.Themes, "green")

A method for using this in a loop is discussed in this question - When using .net MVC RadioButtonFor(), how do you group so only one selection can be made?

0

精彩评论

暂无评论...
验证码 换一张
取 消