开发者

What determines the key in the ModelStateDictionary for Items of Collection Properties

If this is my view model:

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

Then what would be the default error message keys assigned to a ModelStateDictionary for:

  1. ViewModel.SimpleProperty (see update below)
  2. ViewModel.ComplexProperty (see update below)
  3. ViewModel.ComplexProperty.NestedSimpleProperty (see update below)
  4. ViewModel.ComplexPropertyArray (see update below)
  5. ViewModel.ComplexPropertyArray[0]
  6. ViewModel.ComplexPropertyArray[0].NestedSimpleProperty

Update I found this in reflector:

protected internal static string CreateSubPropertyName(string prefix, string propertyName)
{
    if (string.IsNullOrEmpty(prefix))
    {
        return propert开发者_StackOverflowyName;
    }
    if (string.IsNullOrEmpty(propertyName))
    {
        return prefix;
    }
    return (prefix + "." + propertyName);
 }

So, I think that covers everything except for #5 and #6


If you make the NestedSimpleProperty required:

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

and then you have a form in which you have multiple textboxes for this property corresponding to each item in the ComplexPropertyArray collection then the key that will be used for error messages will be ComplexPropertyArray[i].NestedSimpleProperty where i represents the index of the element in the array which contains an empty value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜