集合的验证摘要

时间:2010-03-17 22:53:24

标签: asp.net-mvc asp.net-mvc-2 validationsummary

编辑:将此问题升级为MVC 2.0
使用asp.net MVC 2.0是否存在创建验证摘要的现有方法,该方法对包含集合的模型有意义?如果没有,我可以创建自己的验证摘要

示例模型:

public class GroupDetailsViewModel
{
    public string GroupName { get; set; }
    public int NumberOfPeople { get; set; }
    public List<Person> People{ get; set; }
}

public class Person
{
    [Required(ErrorMessage = "Please enter your Email Address")]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid Email Address")]
    public string EmailAddress { get; set; }

    [Required(ErrorMessage = "Please enter your Phone Number")]
    public string Phone { get; set; }

    [Required(ErrorMessage = "Please enter your First Name")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Please enter your Last Name")]
    public string LastName { get; set; }
}

如果未输入任何内容,则现有摘要<%=Html.ValidationSummary %>将如下所示。

  

在进行下一步之前,必须纠正以下错误   *请输入您的电子邮件地址
  *请输入您的电话号码
  *请输入您的名字
  *请输入您的姓氏
  *请输入您的电子邮件地址
  *请输入您的电话号码
  *请输入您的名字
  *请输入您的姓氏

设计要求插入标题:

  

在进行下一步之前,必须纠正以下错误   人1   *请输入您的电子邮件地址
  *请输入您的电话号码
  *请输入您的名字
  *请输入您的姓氏
  人2
  *请输入您的电子邮件地址
  *请输入您的电话号码
  *请输入您的名字
  *请输入您的姓氏

回答根据Pharcyde的回答。

public static MvcHtmlString NestedValidationSummary(this HtmlHelper helper)
{
    if (helper.ViewData.ModelState.IsValid)
        return MvcHtmlString.Empty;

    // create datastructure to group error messages under a given key (blank key is for general errors)
    var errors = new Dictionary<string,List<string>>();
    foreach (KeyValuePair<string, ModelState> keyPair in helper.ViewData.ModelState)
    {
        foreach (ModelError error in keyPair.Value.Errors)
        {
            //determine the 'key' for the group in which this error belongs
            var key = keyPair.Key.Split(']')[0];
            if (key.Contains("People["))
                key = "Person " + key.Split('[')[1];
            else
                key = string.Empty;

            if(!errors.ContainsKey(key))
                errors.Add(key,new List<string>());
            //now add message using error.ErrorMessage property
            errors[key].Add(error.ErrorMessage);
        }
    }

    // generate the HTML
    var ul = new TagBuilder("ul");
    foreach (KeyValuePair<string, List<string>> errorPair in errors.OrderBy(p=>p.Key))
    {
        var li = new TagBuilder("li");
        if(!string.IsNullOrEmpty(errorPair.Key))
            li.InnerHtml += string.Format("<p class=\"no-bottom-margin\"><strong>{0}</strong></p>",errorPair.Key);
        var innerUl = new TagBuilder("ul");
        foreach (var message in errorPair.Value)
        {
            var innerLi = new TagBuilder("li");
            innerLi.InnerHtml = message;
            innerUl.InnerHtml += innerLi.ToString(TagRenderMode.Normal);
        }
        li.InnerHtml += innerUl.ToString(TagRenderMode.Normal);
        ul.InnerHtml += li.ToString(TagRenderMode.Normal);
    }

    return MvcHtmlString.Create(ul.ToString(TagRenderMode.Normal));
}

1 个答案:

答案 0 :(得分:3)

您将不得不扩展HtmlHelper方法并自行滚动。下面是一些代码,这些代码对于您需要一个小组的情况非常重要:

//HtmlHelper being extended
if(helper.ViewData.ModelState.IsValid)
{
    foreach(KeyValuePair<string,ModelState> keyPair in helper.ViewData.ModelState)
    {     
        //add division for group by here using keyPair.Key property (would be named "Person" in your case).

        foreach(ModelError error in keyPair.Value.Errors)
        {
            //now add message using error.ErrorMessage property
        }
    }
}