Was bestimmt den Schlüssel im ModelStatedictionary für Elemente von Sammeleigenschaften
-
28-10-2019 - |
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:
ViewModel.SimpleProperty(Siehe Update unten)ViewModel.complexproperty(Siehe Update unten)ViewModel.complexproperty.nestedsimpleProperty(Siehe Update unten)ViewModel.complexpropertyArray(Siehe Update unten)- ViewModel.complexpropertyArray [0
- 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
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