将 ModelState 错误复制到 TempData 和 TempData在视图中显示它们
我的大多数操作方法在成功时返回 PartialViews,在失败时返回 RedirectToAction 结果。为此,我想将模型状态错误复制到 TempData 中,以便可以将它们显示给用户。我在这里阅读了几个关于 SO 的问题和一些外部链接,但没有一个对我有用...我正在使用 MvcContrib 中的 ModelStateToTempData
属性装饰 ActionMethod,然后在视图中显示如下:(这只是一个原型)
@if (TempData.Count > 0)
{
foreach (var obj in TempData)
{
var errors = ((ModelStateDictionary)obj.Value).Values;
foreach (var error in errors)
{
<div style="position:absolute; background:Black; color:White; top:250px; left:550px;">
<span style="margin-bottom:5px; display:block; height:25px;">@error.Value</span>
</div>
}
}
}
我不断收到 System.Web.Mvc.ValueProviderResult
,而不是显示错误本身。我知道这都是错误的,最终我想将模型状态错误过滤到 TempData 内的字典中,但现在我只想将错误字符串显示在视图中。
PS:我尝试在没有 MvcContrib 属性的情况下手动执行此操作,并且得到了相同的结果。但我确实更喜欢使用自己的代码,这样我可以更好地控制整个问题。
有什么建议吗?
Most of my action methods return PartialViews on success and RedirectToAction results on failure. For that, I would like to copy the model state errors into TempData so I could display them to the user. I've read several questions here on SO and some external links but none of them worked for me... I'm decorating the ActionMethod with ModelStateToTempData
attribute from MvcContrib, then displaying it as follows in the view: (this is just a prototype)
@if (TempData.Count > 0)
{
foreach (var obj in TempData)
{
var errors = ((ModelStateDictionary)obj.Value).Values;
foreach (var error in errors)
{
<div style="position:absolute; background:Black; color:White; top:250px; left:550px;">
<span style="margin-bottom:5px; display:block; height:25px;">@error.Value</span>
</div>
}
}
}
Rather than displaying the error itself, I keep getting System.Web.Mvc.ValueProviderResult
. I know this is all wrong, and eventually I would want to filter the model state errors into a dictionary inside the TempData but for now I just want to have the error string displayed in the view.
P.S: I've tried to do it manually without the MvcContrib attribute, and I got the same result. But I do prefer to use my own code so I could have more control over the whole issue.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,在尝试了一百万件事之后,我自己找到了答案...:)
在视图中:
更新:
这里它位于 ActionFilter 内:
Ok After trying a million things, I found the answer myself... :)
And in the view:
UPDATE:
Here it is inside an ActionFilter:
我开始沿着这条路走下去,然后阅读你的答案。我将它们合并到以下文件中:
TempDataDictionaryExtensions.cs
我创建了扩展方法来对 TempData 进行肮脏的工作,因为我觉得它不属于操作过滤器本身。
TempDataModelStateAttribute.cs
我的原始版本,在通过
OnResultExecuting
执行 ActionResult 之前,将TempData
中的错误复制回ModelState
中。这是将它们复制到 TempData 并返回的组合。I started going down this road, and then read your answer. I combined them into the following files:
TempDataDictionaryExtensions.cs
I created extension methods to do the dirty work on the TempData, because I felt it didn't belong in the Action Filter itself.
TempDataModelStateAttribute.cs
My original, copied the errors out of
TempData
back intoModelState
prior to the ActionResult executing viaOnResultExecuting
. This is a combination of copying them intoTempData
and back out.你应该认真考虑这个概念:
http ://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx#prg
You should seriously consider this concept:
http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx#prg