我在我的视图模型中有这个属性:
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; }
}
我的观点模型:
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>
答案 0 :(得分:2)
这是因为您的IsObligatory
属性类型为bool
,因此它只能为true或false(默认值为false)。如果将类型更改为bool?
(可为空),则默认值将为null,并且不会选择任何单选按钮。
public bool? IsObligatory { get; set; }