代码之家  ›  专栏  ›  技术社区  ›  David

如何在Html.ValidationSummary中指定数据注释错误的顺序

  •  3
  • David  · 技术社区  · 14 年前

    <%= Html.ValidationSummary("Please review the errors below") %>
    

    有什么方法可以指定错误的显示顺序吗?

    例子:

    public class ClassA { [Required]public string AProperty; }
    public class ClassB : ClassA { [Required]public string BProperty; }
    

    AProperty: <%= Html.TextBoxFor(m => m.AProperty) %>
    BProperty: <%= Html.TextBoxFor(m => m.BProperty) %>
    

    验证错误显示为:

    The BProperty is required.
    The AProperty is required.
    
    4 回复  |  直到 14 年前
        1
  •  -1
  •   John Farrell    14 年前

    不。反射用于获取所有dataannotation,它们总是按照调用 typeof(MagicSocks).GetTYpe().GetProperties() . 在您的例子中,我很确定派生类属性总是出现在基类型属性之前。

    您必须编写自己的助手和属性,才能按您选择的顺序显示验证错误。

        2
  •  2
  •   Yuck    12 年前

    public static void OrderByKeys(this ModelStateDictionary modelStateDictionary, IEnumerable<string> keys)
    {
        ModelStateDictionary result = new ModelStateDictionary();
        foreach (string key in keys)
        {
            if (modelStateDictionary.ContainsKey(key) && !result.ContainsKey(key))
            {
                result.Add(key, modelStateDictionary[key]);
            }
        }
        foreach (string key in modelStateDictionary.Keys)
        {
            if (!result.ContainsKey(key))
            {
                result.Add(key, modelStateDictionary[key]);
            }
        }
        modelStateDictionary.Clear();
        modelStateDictionary.Merge(result);
    }
    

    可通过以下方式使用:

    ModelState.OrderByKeys(new[] { "AProperty", "BProperty" });
    
        3
  •  0
  •   Hasmukh    14 年前

    我不确定我的回答是对是错,你可以这样试试。

       public ActionResult yourAction(your params)
        { 
               if (!ModelState.IsValid)
                {
                    var errs = from er in tmpErrs
                               orderby er.Key
                               select er;
    
                    ModelState.Clear();
    
                    foreach (var err in errs)
                    {
                        ModelState.Add(err);
                    }
                }
        // your code 
    
        }
    
        4
  •  0
  •   Andrew Gunn    11 年前

    using System.Linq;
    using System.Web.Mvc;
    
    namespace 
    {
        public class OrderedModelStateAttribute : FilterAttribute, IActionFilter
        {
            public void OnActionExecuted(ActionExecutedContext filterContext)
            {
                var modelState = filterContext.Controller.ViewData.ModelState;
                var orderedModelState = new ModelStateDictionary();
    
                foreach (var key in filterContext.HttpContext.Request.Form.Keys.Cast<string>()
                                                 .Where(
                                                     key =>
                                                     modelState.ContainsKey(key) && !orderedModelState.ContainsKey(key)))
                {
                    orderedModelState.Add(key, modelState[key]);
                }
    
                foreach (var key in modelState.Keys.Where(key => !orderedModelState.ContainsKey(key)))
                {
                    orderedModelState.Add(key, modelState[key]);
                }
    
                modelState.Clear();
                modelState.Merge(orderedModelState);
            }
    
            public void OnActionExecuting(ActionExecutingContext filterContext)
            {
            }
        }
    }
    

    使用以下代码将筛选器添加到所有操作: filters.Add(new OrderedModelStateAttribute());