猿问

默认情况下,当检查属性是错误时,默认情况下会检查单选按钮吗?

我正在尝试生成单选按钮,如果数据库中存在该值,则有条件地检查选中的属性,那么应该选择它,否则选中的属性为假。所以最初数据库中没有行,并且所有单选按钮的选中属性也为假,但仍然在 UI 上选中,见下图

所以不知道这是默认行为还是留下任何错误,下面是我的代码


视图模型


public class CompanionProductVersion1ViewModel

{

    [Required(ErrorMessage = "Please select companion release - 1")]

    public string CompanionReleaseRadio1 { get; set; }

    public List<CompanionReleaseRadio1> CompanionReleaseRadios1 { get; set; }

}


public class CompanionReleaseRadio1

{

    public string RadioID { get; set; }

    public string RadioText { get; set; }

    public bool Checked { get; set; }

}

看法


@model PlatformLifecyclePortal.Web.ViewModel.CompanionProductVersion1ViewModel


<div class="mt-5">

    <div class="d-flex pb-3">

        <h2>Companion Release - 1</h2>

    </div>


    <div class="border border-dark p-5">

        <div class="row"><h6><b>@Session["Release"]</b></h6></div>

        <div class="row"><p><b>@Session["Feature"]</b></p></div>

        <div class="row">@Html.ValidationMessageFor(m => m.CompanionReleaseRadio1, "", new { @class = "help-block text-danger", @style = "color:red;" })</div>

        <div class="row"><div class="col-sm-9">&nbsp;</div></div>


        <div class="row">


            @using (Html.BeginForm())

            {

                <div class="form-group row">

                    @foreach (var companionReleases1 in Model.CompanionReleaseRadios1)

                    {

                        <div class="col-sm-4">

                            @Html.RadioButtonFor(model => Model.CompanionReleaseRadio1, companionReleases1.RadioID, new { id = "CompanionReleaseRadio2" + companionReleases1.RadioID, @checked = companionReleases1.Checked }) <span class="checkbox-label">@companionReleases1.RadioText</span>

                        </div>

                    }

                </div>


                <div class="row">

                    <div class="col-sm-9">

                        <button type="submit" class="btn btn-primary btn-next">Next</button>

                    </div>

                </div>

            }

        </div>

    </div>

</div>


缥缈止盈
浏览 97回答 2
2回答

拉风的咖菲猫

通过检查checked单选按钮的属性行为,我发现您的问题发生了,因为checked属性是一个布尔属性,当该属性存在时表示相应的输入元素处于选中状态,而不管其值如何(与orchecked="false"相同)。checked="true"checked="checked"如果checked同一组中的多个单选按钮存在属性,则默认情况下它将最后一个单选按钮设置为checked(根据您的原始代码查看此小提琴)。checked因此,当属性设置为 false 时,您需要CompanionReleaseRadio1.Checked使用@functions内联函数帮助器排除属性,如下例所示:@functions {&nbsp; &nbsp; private object SetRadioButtonChecked(bool isChecked, string RadioID)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // checked state from property&nbsp; &nbsp; &nbsp; &nbsp; if (isChecked)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // checked attribute is present&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new { id = "CompanionReleaseRadio2" + RadioID, @checked = "checked" };&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // checked attribute is not present&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new { id = "CompanionReleaseRadio2" + RadioID };&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}RadioButton然后在helper中调用上面的内联函数,如下所示:@foreach (var companionReleases1 in Model.CompanionReleaseRadios1){&nbsp; &nbsp; <div class="col-sm-4">&nbsp; &nbsp; &nbsp; &nbsp; @Html.RadioButtonFor(model => Model.CompanionReleaseRadio1, companionReleases1.RadioID,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@SetRadioButtonChecked(companionReleases1.Checked, companionReleases1.RadioID))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <span class="checkbox-label">@companionReleases1.RadioText</span>&nbsp; &nbsp; </div>}之后,所有CompanionReleaseRadio1.Checked属性设置为的单选按钮false都应该设置为未选中状态(有关预期结果,请参见this fiddle)。

弑天下

我创建了一个 Windows 应用程序并在 Form1 上添加了一个 RadioButton。并将选中的属性设置为单选按钮的“fasle”。但根据 Windows 应用程序的默认行为,它标记为检查了至少一个单选按钮。一旦表单及其控件被加载,那么只有我们可以将单选按钮标记为选中 = false。在 windows 应用程序中,windows 窗体有一个事件 Shown()。所以我们有代码在显示的事件上标记checked = false,它按预期工作。public partial class Form1 : Form{&nbsp; &nbsp; public Form1()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; }&nbsp; &nbsp; private void Form1_Shown(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; radioButton1.Checked = false;&nbsp; &nbsp; }&nbsp;}
随时随地看视频慕课网APP
我要回答