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.
asp.net-mvc model
Simon farrow
source share