I have following code, but validation for the custom type(UserDetails) are not firing. Is there any way to overcome this problem? I know that if i define all the properties of UserDetails inside UserModel, it will work fine. but i need to reuse the UserDetails
Model,
public class UserModel
{
public string Something { get; set; }
public UserDetails User { get; set; }
}
Custom object,
public class UserDetails
{
[Required]
public string FirtstName { get; set; }
[Required]
public string LastName { get; set; }
[StringLength(50, ErrorMessage = "{0} can not be greater than {1} characters")]
public string Address { get; set; }
}
View,
@Html.ValidationSummary(true)
@Html.TextAreaFor(model => model.UserDetails.Address , new { rows = "5", cols = "20"})
@Html.ValidationMessageFor(model => model.UserDetails.Address )
....
This is just because of the ID and name of the element. for example in this case name of the FirtstName control is
UserModel.FirtstNameand ID isUserModel_FirtstNameso client side validation will not fire in this case. if you want to add validation you have to add client validation manually. but you can validate it in the server side by usingModelState.IsValidclient side validation