gpt4 book ai didi

c# - RadioButtonFor 删除默认选中

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:54 26 4
gpt4 key购买 nike

我的 View 模型中有这个属性:

public bool IsObligatory { get; set; }

它应该由用户在 post 方法中填写。问题是我检查了第二个单选按钮的默认值。如何删除它?即使我在我的模型中将 IsObligatory 属性设置为可选的第二个单选按钮,默认情况下也会保持选中状态。

我的模型类与这个问题相关:

public class Subject
{
public int ID { get; set; }
public string Name { get; set; }
public int Semester { get; set; }

public virtual ICollection<SubjectCourse> Courses { get; set; }
}
public class Course
{
public int ID { get; set; }
public string Abbreviation { get; set; }
public string Title { get; set; }

public virtual ICollection<SubjectCourse> Subjects { get; set; }
}
public class SubjectCourse
{
public int SubjectCourseID { get; set; }
public int SubjectID { get; set; }
public int CourseID { get; set; }
public bool? IsObligatory { get; set; }

public virtual Subject Subject { get; set; }
public virtual Course Course { get; set; }
}

我的 View 模型:

public class SubjectVM
{
public int? ID { get; set; }
...
public List<SelectedCourseVM> SelectedCourses { get; set; }
...
}
public class SelectedCourseVM
{
public int ID { get; set; }
public string Title { get; set; }
public bool IsObligatory { get; set; }
public bool Selected { get; set; }
}

我的部分观点:

<div class="form-group adding collapsable-courses">
@for (int i = 0; i < Model.SelectedCourses.Count; i++)
{
<div class="panel-group" id="@Html.Id(@Model.SelectedCourses[i].Title)">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#@Html.Id(@Model.SelectedCourses[i].Title)" href="#@Model.SelectedCourses[i].ID">
@Html.HiddenFor(model => model.SelectedCourses[i].ID)
@Html.CheckBoxFor(model => model.SelectedCourses[i].Selected, new { @class = "checkbox-inline" })
<label class="course-label">@Model.SelectedCourses[i].Title</label>
</a>
</h4>
</div>
<div id="@Model.SelectedCourses[i].ID" class="panel-collapse collapse in courses-to-collapse" toggle="false">
<div class="panel-body">
<div class="isobligatory-radios">
<label class="radio-inline">
@Html.RadioButtonFor(model => model.SelectedCourses[i].IsObligatory, true)
Obavezan predmet
</label>
<label class="radio-inline">
@Html.RadioButtonFor(model => model.SelectedCourses[i].IsObligatory, false)
Izborni predmet
</label>
</div>
</div>
</div>
</div>
</div>
}
</div>

最佳答案

发生这种情况是因为您的 IsObligatory 属性属于 bool 类型,因此它只能是 true 或 false(默认值为 false)。如果将类型更改为 bool?(可为 null),则默认值为 null,并且两个单选按钮都不会被选中。

public bool? IsObligatory { get; set; }

关于c# - RadioButtonFor 删除默认选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28662564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com