当我提交表单时,我的 ViewModel 中的课程列表得到空值。这是我的视图模型:
public class CandidateViewModel
{
public string Name { get; set; }
public IList<CourseViewModel> Courses { get; set; }
public CandidateViewModel()
{
Name = "Kiwanax";
Courses = new List<CourseViewModel>();
for (int count = 0; count < 5; ++count)
{
Courses.Add(new CourseViewModel(
string.Format("Course_{0}", count), 5));
}
}
}
public class CourseViewModel
{
public long ID { get; set; }
public string Name { get; set; }
public OptionViewModel SelectedOption { get; set; }
public IList<OptionViewModel> Options { get; set; }
public CourseViewModel(string name, int numberOfOptions)
{
Name = name;
Options = new List<OptionViewModel>();
for (int count = 1; count <= numberOfOptions; ++count)
{
Options.Add(new OptionViewModel(count.ToString(),
string.Format("Option {0}", count)));
}
SelectedOption = Options[0];
}
}
public class OptionViewModel
{
public string Value { get; set; }
public string Description { get; set; }
}
好的。当我根据我的 IList 创建 DropDownLists 时,它工作正常。
<ul id="cursos">
@for (int count = 0; count < Model.Courses.Count; ++count)
{
<li>
@Html.DropDownListFor(i => i.Courses[count].SelectedOption,
new SelectList(Model.Courses[count].Options, "Value", "Description", Model.Courses[count].SelectedOption))
@Html.HiddenFor(i => i.Courses[count].ID)
@Model.Courses[count].Name
</li>
}
</ul>
但是,当我从我的表单中发布时,我会从该 IList 中获取空值。