2008-12-15 55 views
6

我目前在asp.net mvc中使用ModelStateDictionary來保存驗證錯誤,然後傳回給用戶。能夠通過ModelState.IsValid檢查整個模型是否有效。但是,我正在處理的當前應用程序需要能夠報告警告。這些並不重要,因此表單內容仍可以保存,但應該向用戶顯示,以便可以選擇採取措施。ASP.NET MVC中的模型警告

我一直在尋找通過框架,看看是否有任何明顯的地方來擴大它,讓我做到這一點。我在想另一個帶有警告的字典和一個稱爲模型警告的模型錯誤的子類。我不知道如何讓框架在視圖中使用我的新容器類等,但我仍然想要所有現有的錯誤的東西工作。

如果有人嘗試過類似的東西或有任何想法,我會很感激他們的意見。

更新:

我得儘可能延長的ViewDataDictionary添加一些警告

public class AetherViewDataDictionary : ViewDataDictionary 
{ 
    public AetherViewDataDictionary() 
    { 
     ModelStateWarning = new ModelStateDictionary(); 
    } 

    public AetherViewDataDictionary(object model) : base(model) 
    { 
     ModelStateWarning = new ModelStateDictionary(); 
    } 

    public AetherViewDataDictionary(ViewDataDictionary viewDataDictionary) : base(viewDataDictionary) 
    { 
     ModelStateWarning = new ModelStateDictionary(); 
    } 

    public ModelStateDictionary ModelStateWarning { get; private set; } 
} 

那我現在遇到的問題是,當我得到我的看法代碼,這只是爲了調試我正在失去一個事實,那就是我的新類型,所以當我嘗試拋棄它並訪問我的新字典時,我沒有快樂。

public partial class Index : ViewPage<PageViewData> 
{ 
    protected override void SetViewData(ViewDataDictionary viewData) 
    { 
     base.SetViewData(viewData); 
    } 
} 

它在這裏設置正確,但是當我檢查它的類型消失了。

編輯: 事實證明,這是一種愚蠢的做事方式,請參閱下面的答案。

回答

6

所以,我是領導下來之前,原來是一個壞主意的路線,那裏只是沒有在框架來獲取在您需要的位足夠的訪問。至少不是沒有重新發明輪子幾次。

我決定低着頭擴展的ModelState類的警告集合添加到它的路線:

public class AetherModelState : ModelState 
{ 
    public AetherModelState() { } 

    public AetherModelState(ModelState state) 
    { 
     this.AttemptedValue = state.AttemptedValue; 

     foreach (var error in state.Errors) 
      this.Errors.Add(error); 
    } 

    private ModelErrorCollection _warnings = new ModelErrorCollection(); 

    public ModelErrorCollection Warnings { get { return this._warnings; } } 
} 

爲了能夠輕鬆地在你的錯誤我創建了同樣的方法添加警告一些擴展方法爲ModelStateDictionary:

public static class ModelStateDictionaryExtensions 
{ 
    public static void AddModelWarning(this ModelStateDictionary msd, string key, Exception exception) 
    { 
     GetModelStateForKey(key, msd).Warnings.Add(exception); 
    } 

    public static void AddModelWarning(this ModelStateDictionary msd, string key, string errorMessage) 
    { 
     GetModelStateForKey(key, msd).Warnings.Add(errorMessage); 
    } 

    private static AetherModelState GetModelStateForKey(string key, ModelStateDictionary msd) 
    { 
     ModelState state; 
     if (string.IsNullOrEmpty(key)) 
      throw new ArgumentException("key"); 

     if (!msd.TryGetValue(key, out state)) 
     { 
      msd[key] = state = new AetherModelState(); 
     } 

     if (!(state is AetherModelState)) 
     { 
      msd.Remove(key); 
      msd[key] = state = new AetherModelState(state); 
     } 

     return state as AetherModelState; 
    } 

    public static bool HasWarnings(this ModelStateDictionary msd) 
    { 
     return msd.Values.Any<ModelState>(delegate(ModelState modelState) 
     { 
      var aState = modelState as AetherModelState; 
      if (aState == null) return true; 
      return (aState.Warnings.Count == 0); 
     }); 
    } 
} 

的GetModelStateForKey代碼是舉步維艱,但你應該能夠看到我這個領導。接下來要做的是編寫一些擴展方法,讓我顯示警告和錯誤

+0

什麼是AttemptedValue? – 2015-01-14 10:49:49

4

爲什麼不簡單地向ViewData添加警告列表或字典,然後將它們顯示在視圖中?

例如

ViewData[ "warnings" ] = new[] { "You need to snarfle your aardvark" } ;