2010-03-17 12 views
6

EDIT: aggiornato questa domanda a MVC 2.0
Con asp.net MVC 2.0 esiste un metodo esistente di creazione di riepilogo di convalida che ha senso per i modelli contenenti le collezioni? Se non riesco a creare il mio riassunto di convalidaconvalida Riepilogo per Collezioni

Esempio Modello:

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; } 
} 

La sintesi esistente <%=Html.ValidationSummary %> se non viene immesso simile a questa.

il seguente errore (s) deve essere corretta prima di procedere alla fase successiva
* Inserisci il tuo indirizzo e-mail
* Inserisci il tuo numero di telefono
* Inserisci il tuo Nome
* Inserisci il tuo cognome
* Inserisci il tuo indirizzo e-mail
* Inserisci il tuo numero di telefono
* Inserisci il tuo Nome
* Inserisci il tuo cognome

Il progetto prevede per i titoli da inserire in questo modo:

il seguente errore (s) deve essere corretta prima di procedere alla fase successiva
Persona 1
* Inserisci il tuo indirizzo e-mail
* Inserisci il tuo numero di telefono
* Inserisci il tuo Nome
* Inserisci il tuo cognome
Perso n 2
* Inserisci il tuo indirizzo e-mail
* Inserisci il tuo numero di telefono
* Inserisci il tuo Nome
* Inserisci il tuo cognome

risposta in base alla risposta del 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

Ho appena fatto la stessa domanda bizzarra. Indovina quindi la risposta non è tanto ..... – Terrance

risposta

3

È necessario estendere i metodi HtmlHelper e eseguire il rollover. Ecco il pezzetto di codice che è importante per la tua situazione in cui hai bisogno di un gruppo:

//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 
     } 
    } 
} 
+0

Sì, ho scritto il mio basandomi sulla tua risposta, l'ho inclusa nella domanda. – Myster

Problemi correlati