在ASP.NET MVC模式的警告 [英] Model Warnings in ASP.NET MVC

查看:138
本文介绍了在ASP.NET MVC模式的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的ModelStateDictionary在asp.net mvc的持有验证错误,然后传回给用户。能够检查整个模型是ModelState.IsValid有效特别。然而,我的工作电流应用程序需要能够报告警告。这些都不是作为关键的,形式的内容仍然可以保存,但它们应该显示给用户以便采取行动可以任选采取

I'm currently using the ModelStateDictionary in asp.net mvc to hold validation errors and pass then back to the user. Being able to check if the whole model is valid with ModelState.IsValid is particularly. However, a current application I'm working on has a need to be able to report warnings. These aren't as critical so the form content can still be saved, but they should be shown to the user so that action can be optionally taken.

我一直在寻找通过框架,看看是否有把它扩大到允许我做这件事的任何明显的地方。我在想,在警告和模型误差的一个子类另一个字典被称为模型警告。我不知道我怎么会得到的框架,用我的新容器类视图等。不过,我还是希望所有的现有的错误的东西一起工作。

I've been looking through the framework to see if there are any obvious place to extend it to allow me to do this. I'm thinking that another dictionary with warnings in and a subclass of model error called model warning. I'm not sure how I'd get the framework to use my new container classes in the view etc. though, I still want all of the existing error stuff to work.

如果任何人试图类似的话,或有我最好的想法AP preciate他们的意见。

If anyone has tried anything similar or has any thoughts I'd appreciate their input.

更新:

我得尽可能延长的ViewDataDictionary添加一些警告

I've got as far as extending the 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; }
}

这是我现在遇到的问题是,当我得到我的看法code,这只是调试我失去的事实,它的我的新类型,所以当我尝试投它回来,可以访问我的新词典我不快乐。

The problem that I'm having now is that when I get to my view code, this is just for debug I'm losing the fact that its my new type, so when I try to cast it back and get access to my new dictionary I have no joy.

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

它设置它正确这里,但是当我检查类型的走了。

It sets it correctly here, but when I check the type its gone.

编辑:
这原来是做事情的方式哑巴,见下面的回答。

This turned out to be a dumb way of doing things, see answer below.

推荐答案

所以,我是领导下来之前横空出世的路线是一个坏主意,但只是没有在框架足够的访问来获得在该位你需要。至少在没有几次重新发明轮子。

So the route that I was headed down before turned out to be a bad idea, there just isn't enough access in the framework to get at the bits that you need. At least not without reinventing the wheel a few times.

我决定低着头扩展的ModelState类将警告集合添加到它的路线:

I decided to head down the route of extending the ModelState class to add a warnings collection 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; } }
}

为了能够很容易地以相同的方式添加的警告,你会错误我创建了ModelStateDictionary一些扩展方法:

In order to be able to easily add warnings in the same way that you would errors I created some extension methods for the 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 code是举步维艰,但你应该能够看到我这个领导。接下来要做的事情就是写一些扩展方法,让我随着误差

The GetModelStateForKey code is ropey but you should be able to see where I'm headed with this. The next thing to do is to write some extension methods that allow me to display the warnings along with the errors

这篇关于在ASP.NET MVC模式的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆