2010-06-03 31 views
5

我使用DA 4.0與MVC應用程序並創建了一個自定義的驗證,如下圖所示:使用DataAnnotations 4.0

public static ValidationResult NumberOfItems(int numItems, ValidationContext pValidationContext) 
{ 
    if (numItems == 1) 
    { 
     //Tag as critical error 
     //return new ValidationResult... 
    } 

    if (numItems > 1 && numItems <= 10) 
    { 
     //Tag as non critical error 
    } 

    //Else it's successful 
    return ValidationResult.Success; 
} 

我想標記錯誤消息嚴重錯誤或沒有。如果這不是嚴重錯誤,我想在我的視圖中訪問它,並以不同的方式呈現它。

因此,有2個環節進行:

  1. 標籤失效不同類型的自定義驗證
  2. 修改默認的模型綁定器,以確定關鍵錯誤

我會怎麼做這個?

回答

2

你的兩個問題都需要重寫一大堆MVC的內部錯誤處理代碼。沒有簡單的路徑,我可以看到將錯誤嚴重性添加到所有不同的地方ModelState和ViewModel驗證發生。

「我該怎麼做?」的唯一答案。是「有很多自定義代碼」。 ;)

+0

如果我不使用dataannotations並使用不同的驗證框架會怎麼樣? – 2010-06-03 20:05:46

+1

@Dave Schilling然後你需要問一個不同的問題。 ;) – jfar 2010-06-04 13:17:25