2

これが私のビューモデルの場合:

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

次に、ModelStateDictionaryfor に割り当てられるデフォルトのエラー メッセージ キーは次のようになります。

  1. ViewModel.SimpleProperty (以下の更新を参照)
  2. ViewModel.ComplexProperty (以下の更新を参照)
  3. ViewModel.ComplexProperty.NestedSimpleProperty (以下の更新を参照)
  4. ViewModel.ComplexPropertyArray (以下の更新を参照)
  5. ViewModel.ComplexPropertyArray[0]
  6. ViewModel.ComplexPropertyArray[0].NestedSimpleProperty

更新 リフレクターでこれを見つけました:

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

#5 と #6を除くすべてを網羅していると思います。

4

1 に答える 1

2

必要な場合NestedSimpleProperty

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

次に、コレクション内の各項目に対応するこのプロパティの複数のテキスト ボックスがあるフォームがある場合ComplexPropertyArray、エラー メッセージに使用されるキーは、空の値を含む配列内の要素のインデックスを表すComplexPropertyArray[i].NestedSimpleProperty場所になります。i

于 2011-03-21T14:27:29.617 に答える