Does the ASP.NET MVC 2 model validation includes subobjects?
I have an instance “Filter”, from this class:
public class Filter
{
[StringLength(5)]
String Text { get; set; }
}
in my main object:
public class MainObject
{
public Filter filter;
}
However, when I do TryValidateModel(mainObject) the validation still works out even if “Text” in MainObject.Filter.Text is longer than 5 characters.
Is this intended, or am I doing something wrong?
Two remarks:
I think that the first remark needs not much explanation:
As for the second, here’s when it does not work:
And here’s when it will work:
Of course in this case your code could be simplified to:
Conclusion: you rarely need
TryValidateModel.