停止Html.ValidationSummary()更改验证消息的顺序 [英] Stop Html.ValidationSummary() from changing the order of validation messages
问题描述
在ValidationSummary中,为什么显示消息的顺序与将错误添加到ModelState的顺序不同,我该如何解决?
asp.net mvc是开源的,因此您可以直接查看ValidationSummary的代码.
http://www.asp.net/mvc/download/ >
也就是说,我很确定ModelState是一个字典.因此,如果ValidationSummary遍历ModelState词典中的键/值以查找错误,则顺序将是随机的.
我在工作时下载了代码.来自MVC/Html/ValidationExtensions.cs中的ValidationSummary:
foreach (ModelState modelState in htmlHelper.ViewData.ModelState.Values) {
foreach (ModelError modelError in modelState.Errors) {
// stuff to build a string with the error
}
}
因此它正在遍历字典中的值.从MSDN:
出于枚举目的,字典中的每个项目都被视为代表值及其键的KeyValuePair(TKey,TValue)结构. 退回商品的顺序不确定.
和
Dictionary(TKey,TValue).ValueCollection中的值顺序为未指定
强调我的.
In a ValidationSummary, why are the messages shown in a different order than the order you added errors to the ModelState and how might I fix this?
asp.net mvc is open source so you can see the code for ValidationSummary directly.
http://www.asp.net/mvc/download/
That said, I'm pretty sure ModelState is a dictionary. So if ValidationSummary is iterating over the key/values in the ModelState dictionary looking for errors the order is going to be random.
I downloaded the code at work. From ValidationSummary in MVC/Html/ValidationExtensions.cs:
foreach (ModelState modelState in htmlHelper.ViewData.ModelState.Values) {
foreach (ModelError modelError in modelState.Errors) {
// stuff to build a string with the error
}
}
So it is iterating over the values in the dictionary. And from MSDN:
For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair(TKey, TValue) structure representing a value and its key. The order in which the items are returned is undefined.
and
The order of the values in the Dictionary(TKey, TValue).ValueCollection is unspecified
Emphasis mine.
这篇关于停止Html.ValidationSummary()更改验证消息的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!