选中时默认选中单选按钮 属性 为 false?
Radio button is checked by default when checked property is false?
我正在尝试生成单选按钮,有条件地检查选中的 属性 如果该值存在于数据库中,则应选择它,否则选中 属性 为假。所以最初数据库中没有行,并且选中的 属性 对于所有单选按钮也是错误的,但它仍然在 UI 上被选中,见下图
所以不知道这是默认行为还是遗留的任何错误,下面是我的代码
ViewModel
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"> </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>
我创建了一个 windows 应用程序并在 Form1 上添加了一个 RadioButton。
并将选中的 属性 设置为单选按钮的 "fasle"。但根据 windows 应用程序的默认行为,它至少标记了一个单选按钮。
一旦加载了表单及其控件,我们就只能将单选按钮标记为 checked = 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;
}
}
通过检查 checked
attribute behavior for radio button,我发现您的问题发生是因为 checked
属性是一个布尔值 属性,当该属性存在时表示相应的输入元素处于选中状态,而不管它的值(checked="false"
与 checked="true"
或 checked="checked"
相同)。
如果同一组中的多个单选按钮存在 checked
属性,默认情况下它会将最后一个单选按钮设置为 checked
(根据您的原始代码参见 this fiddle)。因此,当 CompanionReleaseRadio1.Checked
属性 设置为 false 时,您需要排除 checked
属性,方法是使用 @functions
内联函数帮助程序,如下例所示:
@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
帮助程序中调用上面的内联函数,如下所示:
@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
我正在尝试生成单选按钮,有条件地检查选中的 属性 如果该值存在于数据库中,则应选择它,否则选中 属性 为假。所以最初数据库中没有行,并且选中的 属性 对于所有单选按钮也是错误的,但它仍然在 UI 上被选中,见下图
所以不知道这是默认行为还是遗留的任何错误,下面是我的代码
ViewModel
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"> </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>
我创建了一个 windows 应用程序并在 Form1 上添加了一个 RadioButton。 并将选中的 属性 设置为单选按钮的 "fasle"。但根据 windows 应用程序的默认行为,它至少标记了一个单选按钮。
一旦加载了表单及其控件,我们就只能将单选按钮标记为 checked = 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;
}
}
通过检查 checked
attribute behavior for radio button,我发现您的问题发生是因为 checked
属性是一个布尔值 属性,当该属性存在时表示相应的输入元素处于选中状态,而不管它的值(checked="false"
与 checked="true"
或 checked="checked"
相同)。
如果同一组中的多个单选按钮存在 checked
属性,默认情况下它会将最后一个单选按钮设置为 checked
(根据您的原始代码参见 this fiddle)。因此,当 CompanionReleaseRadio1.Checked
属性 设置为 false 时,您需要排除 checked
属性,方法是使用 @functions
内联函数帮助程序,如下例所示:
@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
帮助程序中调用上面的内联函数,如下所示:
@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