c# - Model binding and validation in WebAPI -


binding

when try post model via

[httppost] public ihttpactionresult dopost(mymodel model) 

with simple model

public class mymodel  {     public string property{ get; set; } } 

then model not being bound properly. gets instantiated, mymodel.property null. thing helped define model [datacontract], doesn't seem required according documentation (e.g. here)

as want actual representation differ property name use name data member. want field required field:

[datacontract] public class mymodel  {     [datamember(name="property", isrequired=true)]     public string property{ get; set; } } 

validation

now if try validate model using actionfilterattribute, however, isrequired property of attribute ignored. thing works [required]:

[datacontract] public class mymodel  {     [datamember(name="property")]     [required]     public string property{ get; set; } } 

this leads next problem! requiredattribute ignores name="property" , requires property named property, validation fails if post { "property": "foo" }

so end is:

[datacontract] public class mymodel  {     [datamember(name="property")]     [displayname("property")]     [required]     public string property{ get; set; } } 

this seems awkward , don't want models. please help.


Comments