Asp.net mvc 3 Asp.net MVC3下拉列表验证

Asp.net mvc 3 Asp.net MVC3下拉列表验证,asp.net-mvc-3,validation,html-select,Asp.net Mvc 3,Validation,Html Select,型号: 控制器: public class Product { public string Title { get; set; } [Required] public int CategoryId { get; set; } //or public int? CategoryId { get; set; } } @Html.DropDownListFor(m => m.CategoryId , new SelectList(ViewBag.C

型号:

控制器:

public class Product
{
    public string Title { get; set; }

    [Required]
    public int CategoryId { get; set; } 
    //or
    public int? CategoryId { get; set; } 
}
@Html.DropDownListFor(m => m.CategoryId , new SelectList(ViewBag.Categories , "CategoryId ", "Title"),"")
@Html.ValidationMessageFor(model => model.CategoryId )
视图:

我有客户端验证

但在Microsoft默认脚手架中:

控制器:

public class Product
{
    public string Title { get; set; }

    [Required]
    public int CategoryId { get; set; } 
    //or
    public int? CategoryId { get; set; } 
}
@Html.DropDownListFor(m => m.CategoryId , new SelectList(ViewBag.Categories , "CategoryId ", "Title"),"")
@Html.ValidationMessageFor(model => model.CategoryId )
视图:


这是一个非常好和干净的代码。但是客户端验证不起作用。如何使用Microsoft默认脚手架语法和客户端验证只需使CategoryId属性为空即可

@Html.DropDownList("CategoryId" ,"")
[Required]
public int? CategoryId { get; set; }