开发者

asp.net mvc nested object validation message

  1. 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?

  2. 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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜