【问题标题】:Radio button is checked by default when checked property is false?默认情况下检查单选按钮是否存在错误?
【发布时间】:2019-06-10 04:34:53
【问题描述】:

我正在尝试生成单选按钮,如果数据库中存在该值,则有条件地检查选中的属性,那么应该选择它,否则选中的属性为假。所以最初数据库中没有行,并且所有单选按钮的选中属性也为 false,但仍然在 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>

【问题讨论】:

  • 而不是.Where(...).Count() &gt; 1 ? true : false,最好使用.Any(...),如果有的话会返回true...

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

通过检查checked attribute behavior for radio button,我发现您的问题出现了,因为checked 属性是一个布尔属性,当该属性存在时表示相应的输入元素处于选中状态,而不管其值如何(checked="false" 与@ 相同987654330@ 或 checked="checked")。

如果同一组中的多个单选按钮存在checked 属性,默认情况下它将最后一个单选按钮设置为checked(根据您的原始代码参见this fiddle)。因此,当CompanionReleaseRadio1.Checked 属性设置为false 时,您需要使用@functions 内联函数助手排除checked 属性,如下例所示:

@functions {
    private object SetRadioButtonChecked(bool isChecked, string RadioID) 
    {
        // checked state from property
        if (isChecked)
        {
            // checked attribute is present
            return new { id = "CompanionReleaseRadio2" + RadioID, @checked = "checked" };
        }
        else
        {
            // checked attribute is not present
            return new { id = "CompanionReleaseRadio2" + RadioID };
        }
    }
}

然后像这样在RadioButton helper 中调用上面的内联函数:

@foreach (var companionReleases1 in Model.CompanionReleaseRadios1)
{
    <div class="col-sm-4">
        @Html.RadioButtonFor(model => Model.CompanionReleaseRadio1, companionReleases1.RadioID, 
             @SetRadioButtonChecked(companionReleases1.Checked, companionReleases1.RadioID)) 
        <span class="checkbox-label">@companionReleases1.RadioText</span>
    </div>
}

之后,所有CompanionReleaseRadio1.Checked 属性设置为false 的单选按钮都应设置为未选中状态(有关预期结果,请参阅this fiddle)。

参考:

Using @functions in an ASP.NET page with Razor Syntax

相关问题:

How to create a function in a cshtml template?

asp.net mvc3 checking radio button based on model

【讨论】:

    【解决方案2】:

    我创建了一个 Windows 应用程序并在 Form1 上添加了一个 RadioButton。 并将选中的属性设置为单选按钮的“fasle”。但根据 Windows 应用程序的默认行为,它标记为至少选中一个单选按钮。

    一旦表单及其控件被加载,那么只有我们可以将单选按钮标记为选中 = false。

    在 windows 应用程序中有一个用于 windows 窗体的事件 Shown()。所以我们有代码在显示的事件上标记 checked = false 并且它按预期工作。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Shown(object sender, EventArgs e)
        {
            radioButton1.Checked = false;
        }
     }
    

    【讨论】:

    • 我们如何在 ASP.NET MVC 中做到这一点? OP 指的是 Web 应用程序。
    猜你喜欢
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2021-11-18
    • 2019-03-29
    • 2016-08-28
    • 1970-01-01
    相关资源
    最近更新 更多