2012-05-28 54 views
3

驗證工作的其他輸入類型的文本元素,但不工作的DropDownListFor驗證DropDownListFor不適用於MVC3?

類採購輸入屬性代碼

[Required] 
public string LedgerId { get; set; } 

類視圖模型代碼

PurchaseViewModel purchaseVM = new PurchaseViewModel 
{ 
    // PurchaseInput=purchaseInput, 
    Ledger = uw.LedgerRepository.Get().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.LedgerName }), 
}; 

查看

<div class="column"> 
    <div class="labelField"> 
     @Html.LabelFor(model => model.PurchaseInput.LedgerId, "Party") 
    </div> 
    <div class="ItemField"> 
     @Html.DropDownListFor(model => model.PurchaseInput.LedgerId, new SelectList(Model.Ledger, "Value", "Text")) 
     @Html.ValidationMessageFor(model => model.PurchaseInput.LedgerId) 
    </div> 
</div> 

回答

1

在它的面前,看來你沒有在你的選擇列表中的空項。只有當用戶選擇字符串長度爲零的下拉項時,纔會觸發驗證。如果您檢查Html源代碼,您是否可以在下拉列表中看到驗證屬性(取決於您是否使用不顯眼的驗證)?

0

雖然這是一種解決方法,但至少會引發某種驗證。試試:

@Html.DropDownListFor(model => model.PurchaseInput.LedgerId, new SelectList(Model.Ledger, "Value", "Text"), new { @class = "required" })