Frage

Wenn dies mein Ansichtsmodell ist:

 public class ViewModel{
      public string SimpleProperty{get;set;}
      public SubViewModel ComplexProperty{ get;set;}
      public SubViewModel[] ComplexPropertyArray{ get; set; }
 }

 public class SubViewModel{
      public string NestedSimpleProperty{get;set;}
 }

Was wäre dann die Standardfehlermeldungschlüssel, die a zugewiesen wurden? ModelStateDictionary zum:

  1. ViewModel.SimpleProperty (Siehe Update unten)
  2. ViewModel.complexproperty (Siehe Update unten)
  3. ViewModel.complexproperty.nestedsimpleProperty (Siehe Update unten)
  4. ViewModel.complexpropertyArray (Siehe Update unten)
  5. ViewModel.complexpropertyArray [0
  6. ViewModel.complexpropertyArray [0] .NestedSimpleProperty

Aktualisieren Ich fand das in Reflektor:

protected internal static string CreateSubPropertyName(string prefix, string propertyName)
{
    if (string.IsNullOrEmpty(prefix))
    {
        return propertyName;
    }
    if (string.IsNullOrEmpty(propertyName))
    {
        return prefix;
    }
    return (prefix + "." + propertyName);
 }

Ich denke also, das deckt alles ab außer auf #5 und #6

War es hilfreich?

Lösung

Wenn Sie das machen NestedSimpleProperty erforderlich:

public class SubViewModel
{
    [Required]
    public string NestedSimpleProperty{ get; set; }
}

Und dann haben Sie ein Formular, in dem Sie mehrere Textboxen für diese Eigenschaft haben, die jedem Element in der Artikeln entsprechend ist ComplexPropertyArray Sammlung dann der Schlüssel, der für Fehlermeldungen verwendet wird ComplexPropertyArray[i].NestedSimpleProperty wo i Repräsentiert den Index des Elements im Array, der einen leeren Wert enthält.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top