我使用jQuery Unobrtusive验证与MVC3验证DropDownListFor
表单元素,但它不起作用。验证MVC3中的DropDownListFor元素
它验证得很好,如果我修改DropDownListFor
到TextBoxFor
- 它也验证窗体的其他字段。根据数据库中可用的内容,我有多个DropDownListFor
元素。因此,循环。
下面是我的一些代码:
视图模型:
public class ParentViewModel
{
// some other stuff here public
List<Children> Children { get; set; }
}
public class ChildrenViewModel
{
public SelectList PossibleNames { get; set; }
[Required(ErrorMessage = "Select a name")]
public int ChosenNameId { get; set; }
}
查看:
@for (int i = 0; i < Model.Children.Count; i++)
{
@Html.LabelFor(model => modell.Children[i].ChosenNameId, "Name")
@Html.ValidationMessageFor(model => modell.Children[i].ChosenNameId)
@Html.DropDownListFor(model => modell.Children[i].ChosenNameId,
Model.Children[i].PossibleNames, "Choose a name")
}
希望这些提取物足以识别错误...
任何提示为什么这不起作用?
嗯,它仍然无法正常工作。该名称不可空;它需要被选中。 – Helge