Data Validation on nested types in asp.net mvc3
I have following models.
public class RoleViewModel
{
[DisplayName("Role Name")]
[Required(ErrorMessage="Role Name is required.")]
[WhiteSpaceAttribute(ErrorMessage="Name cannot contain white space")]
[StringLength(25, ErrorMessage="Name cannot be be greater than 25 characters")]
public string RoleName { get; set; }
[StringLength(100, ErrorMessage = "Description should be less than 100 characters.")]
public string Description { get; set; }
[DefaultValue("")]
public string DisplayMessage { get; set; }
public bool IsSuccess { get; set; }
}
and
public class RoleDetailsModel
{
public RoleViewModel RoleViewModel { get; set; }
public IList<RoleResourceModel> RoleResources { get; set; }
}
Here, when I am using the RoleViewModel
while creating a role, all the validations are executed as expected. However when I use the RoleDetailsModel
(for edit), none of the validations are executed.
From here it开发者_运维问答 seems that validations are not executed for nested types. Please let me know if my understanding is correct.
If this is the case, How do I implement the above case? Simple way is to add the same validation in RoleDetailsModel
. But this will violate the DRY (Don't Repeat Yourself).
UPDATE: Actually I had missed the ModelState.IsValid
code in controller. I have added that and now it is working fine on server side.
Thanks for clearing my understanding on the nested types part.
精彩评论