-1

我正在开发一个 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 个字符长。

4

1 回答 1

0

我相信这是因为 jquery 正在检查所选项目的数量。

maxlength: function(value, element, param) {
    return this.optional(element) || this.getLength($.trim(value), element) <= param;
}

getLength: function(value, element) {
    switch( element.nodeName.toLowerCase() ) {
        case 'select':
            return $("option:selected", element).length;
        case 'input':
            if( this.checkable( element) )
                return this.findByName(element.name).filter(':checked').length;
    }
    return value.length;
}
于 2013-04-19T17:38:26.843 回答