要求验证不适用于MVC4中的下拉列表吗?

时间:2015-03-03 06:56:09

标签: c# validation asp.net-mvc-4

下拉列表未触发的必填字段验证

型号:

public partial class User
{
    public User()
    {
        this.Mobilizations = new HashSet<Mobilization>();
    }    


    [Required(ErrorMessage="Select Designation")]
    public int? DESIGNATIONID { get; set; }

}

视图模型:

public class userViewModel
{
    public User User { get; set; }

    [DataType(DataType.Password)]
    [Required(ErrorMessage = "CONFIRM PASSWORD is required")]
    [Display(Name="CONFIRM PASSWORD")]      
    [Compare("PASSWORD",ErrorMessage="Password didnot match")]
    public string ComparePassword { get; set; }

    public IEnumerable<Designation> DesignationList { get; set; }
}

控制器:

 public ActionResult Create()
    {           

        var model = new userViewModel
        {
            DesignationList = _db.Designations.ToList()

        };
        return View(model);
    }

查看:

@Html.DropDownListFor(model=>model.User.DESIGNATIONID,new SelectList(Model.DesignationList,"ID","DESIGNATION"),"--Select Designation--",
new { @class = "chosen-select " })
@Html.ValidationMessageFor(model => model.User.DESIGNATIONID)

除了下拉列表外,所有其他验证都会被触发。可能是什么问题?

0 个答案:

没有答案
相关问题