Model Alerts in ASP.NET MVC

I am currently using ModelStateDictionary in asp.net mvc to check for validation errors and pass back to the user. The ability to verify that the entire model is valid with ModelState.IsValid. However, the current application I'm working on should be able to report alerts. They are not so critical, so the contents of the form can be saved, but they must be shown to the user so that an arbitrary action can be performed.

I looked through the structure to see if there was any obvious place to expand it so that I could do it. I think that in another dictionary with warnings and a subclass of the model, the error is called the warning model. I'm not sure how to get the structure to use my new container classes in the view, etc., although I still want all existing error files to work.

If someone tried something like this or had any thoughts, I would appreciate their input.

Update:

I got to the extension of ViewDataDictionary to add some warnings

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; } } 

The problem that I am facing right now is that when I get to my view code, it is just for debugging. I lose the fact that my new type, so when I try to drop it and gain access to my new dictionary I have no joy.

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

Here it correctly sets it, but when I check its type.

Edit: This turned out to be a stupid way of doing things, see Answer below.

+6
asp.net-mvc model
source share
2 answers

Thus, the route I was heading down turned out to be a bad idea, there is simply not enough access within the framework to get the right bits. At least not reinventing the wheel several times.

I decided to skip the extension route of the ModelState class to add a collection of warnings to it:

 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; } } } 

To be able to easily add warnings as well as errors, I created some extension methods for 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); }); } } 

The GetModelStateForKey code is ropey, but you should be able to see what I came across. The next thing to do is write some extension methods that will allow me to display warnings along with errors

+6
source share

Why not just add a warning list or dictionary to ViewData and then display them in your view?

eg.

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

All Articles