asp.net-mvc ASP.NET MVC Razor 中的默认单选按钮列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5206528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:58:11  来源:igfitidea点击:

Default radiobuttonlist in ASP.NET MVC Razor

asp.net-mvcrazorwebforms

提问by jaffa

How do I ensure that a radio button list has the first option selected? If it is based on the model property, how is this set if this is the first page that is shown on application start-up?

如何确保单选按钮列表选择了第一个选项?如果是基于模型属性,如果这是应用程序启动时显示的第一页,如何设置?

This is easy peasy in webforms but I don't know how to do this in MVC and there doesn't seem to be anywhere on the NET which shows how this is done.

这在 webforms 中很容易,但我不知道如何在 MVC 中执行此操作,并且 NET 上似乎没有任何地方显示这是如何完成的。

This doesn't seem to work:

这似乎不起作用:

@Html.RadioButtonFor(m => m.SearchType, true) Location
@Html.RadioButtonFor(m => m.SearchType, false) Name

I Just get both radio buttons unselected??

我只是未选中两个单选按钮??

回答by Linkgoron

@Html.RadioButtonFor(x => x.Something, "radioValue", new { @checked = true })

回答by HaBo

@Html.RadioButtonFor(x => x.Something, "radioValue", new { @checked = "checked"})

or if you are binding from model then

或者如果你是从模型绑定然后

 @Html.RadioButtonFor(x => x.Something, "radioValue", new { @checked = Model.checked})

if we try something like this where x.checked is binding from the database. irrespective of true or false. last radio-button in the list is checked, rest of them are unchecked.

如果我们尝试这样的事情,其中​​ x.checked 是从数据库绑定的。不管是真是假。列表中的最后一个单选按钮被选中,其余的未被选中。

how can we bind the Radio button list from Database and have it worked correctly?

我们如何从数据库绑定单选按钮列表并使其正常工作?

回答by GeorgeMR

This was my solution:

这是我的解决方案:

@Html.RadioButtonFor(model => model.Coin , "Dollar", Model.Coin == "$" ? (new { @checked = "checked" }) : null)
@Html.RadioButtonFor(model => model.Coin , "Euro", Model.Coin == "E" ? (new { @checked = "checked" }) : null)

回答by Hamit YILDIRIM

Very interesting status, i agree both of above, for users who looking also checkbox can use below examples, and for radio button default i reversed the array, in my case below is right!

非常有趣的状态,我同意以上两者,对于同时查看复选框的用户可以使用以下示例,对于单选按钮默认值,我反转了数组,在我的情况下,下面是正确的!

        <div class="row">
            <div class="col-xs-6">
                @for (int idx = 0; idx < Model.TldList.Count; idx++)
                {
                    <div>
                        @Html.HiddenFor(x => Model.TldList[idx].Id)
                        @Html.CheckBoxFor(x => Model.TldList[idx].IsSelected)
                        @Html.DisplayFor(x => Model.TldList[idx].Name)
                    </div>
                }
            </div>
            <div class="col-xs-6">
                @for (int idx = Model.Period.Count-1; idx >= 0; idx--)
                {
                    <div>
                        @Html.RadioButtonFor(x => Model.Period, Model.Period[idx].Id, new { @checked = Model.Period[idx].IsSelected })
                        <span>@Model.Period[idx].Name</span>
                    </div>
                }
            </div>
        </div>