我正在开发一个 MVC4 项目,我有一些奇怪的行为,jQuery 验证正在检查下拉列表中的文本而不是值:
// field in model
[Required]
[DataType(DataType.Text)]
[StringLength(2, ErrorMessage = "The {0} must be {2} characters long.", MinimumLength = 2)]
[Display(Name = "State")]
public string StateCode { get; set; }
// source of states -> List.States
Dictionary<string, string> states = new Dictionary<string, string>();
states.Add("AK", "Alaska");
states.Add("AZ", "Arizona");
// etc.
states.Add("WI", "Wisconsin");
states.Add("WY", "Wyoming");
}
// in the controller
ViewBag.States = new SelectList(Lists.States(), "Key", "Value", Lists.States["AK"]);
<!-- in the view -->
<li>
@Html.LabelFor(m => m.StateCode)
@Html.DropDownListFor(m => m.StateCode, (SelectList)ViewBag.States, "Alaska")
</li>
这是我收到的错误消息: •State 必须是 2 个字符长。