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