odataasp.net-web-api2asp.net-mvc-5.1

How to exclude navigation properties validations from model state on web api


I am using Asp.net MVC 5 with Web API 2 and OData. I am using ODataControllers in web api. I have a contact class which have a navigation property company and company have a required attribute on its name property. On web api contact controller's post action i am checking ModelState.IsValid before saving the data. Now Problem is that when i post contact data on web api its ModelState.IsValid property returns false every time because Company Navigation Property have a property Name with Required attribute. so Model state throws error Name field is required. So please help me to exclude company navigation property from validation check. Hoping for some positive responses. Thanks


Solution

  • @FengZhao-MSFT Thanks for your reply but i found the main cause of this problem. This is occuring because when i am posting contact object it also sends company="", companyID=1 in posted data where company is navigation property of contact. so this company="" is creating the problem due to this when data goes to post method it creates a new instance of company and throws model state error for name field required. If we remove company="" from our posted data and only sends companyID problem solves. One more thing we are using a common ajax post method for all our resources so we can't exclude comapny="" from our request. Now we have excluded it on server side using actionfilter where we have removed company validation errors from modelstate. Thanks