2012-07-09 189 views
0

型號:Asp.net MVC3 DROPDOWNLIST驗證

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

    [Required] 
    public int CategoryId { get; set; } 
    //or 
    public int? CategoryId { get; set; } 
} 

控制器:

ViewBag.Categories = db.Categories.ToList(); 

查看:

@Html.DropDownListFor(m => m.CategoryId , new SelectList(ViewBag.Categories , "CategoryId ", "Title"),"") 
@Html.ValidationMessageFor(model => model.CategoryId) 

而且我有客戶端驗證

但微軟默認腳手架:

控制器:

ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId ", "Title"); 

查看:

@Html.DropDownList("CategoryId" ,"") 

這是非常好的,乾淨的代碼。但客戶端驗證不工作。如何使用微軟默認腳手架的語法和我有客戶端驗證

回答

0

只是讓你的CategoryId屬性爲空的

[Required] 
public int? CategoryId { get; set; } 
+0

我測試。但不工作 – Morteza 2012-07-09 14:41:55