开发者

In ASP.Net MVC 3, how do indicate that properties of child objects have error while writing a custom model validator?

开发者 https://www.devze.com 2023-02-05 03:57 出处:网络
I have the following Model: public class Person { public string Name {get;set;} public Address Address {get;set;}

I have the following Model:

public class Person
{
   public string Name {get;set;}
   public Address Address {get;set;}
}

public class Address
{
   public string Street {get;set;}
   开发者_如何学Cpublic string City {get;set}
}

I have now created a ModelValidator for which validates Person objects. The ModelValidator has one method to implement:

public abstract System.Collections.Generic.IEnumerable<ModelValidationResult> 
Validate(object container)

In this case how do I indicate that an error has occurred for the Address.Street property of the Person object. I have tried setting the ModelValidationResult.MemberName to Address.Street but this is not working?

How do I accomplish this?


Take a look at this blog post for an example of a custom ModelValidator...

It looks like they're making use of the ModelMetadata to handle the property names


My bad, it does work.

In the above example, setting the Member property of the Model Validation Error to "Address.City" does highlight the city field.

Thanks

0

精彩评论

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