asp.net mvc nested object validation message
I need to show all messages for nested child object of parent. Here is mo开发者_开发百科del simplfied
class Parent { Child SomeChild } class Child: IValidatableObject { [Required] string Address; [Required] string City } @Html.ValidationMessageFor(parent => parent.SomeChild)
It doesn't works. Of course
@Html.ValidationMessageFor(parent =>parent.SomeChild.Address)
works but in this case I should do this for every property. Or do reflection... Also I am not sure about
IValidatableObject
interface. How to extract its message?1st was simplified problem. Lets complicate it. Lets say that Child object is passed in Partial view where it is rendered. Partial view model includes also other properties for combo boxes to transfer them to partial view. They are filled in parent controller
@Html.Partial("ChildPartial", new ChildPartialModel(Model.SomeChild,SelectList, SelectListN)
How I could do show error message in such partial?
UPDATE second issue resolved. In partial path in ValidationMessageFor should be like in parent. So if in parent we has Model.Data.Child but in partial we has Model.Child we should use Model.Data,Child. As lambdas are not possible in this case I have used ValidationMessage with text string
精彩评论