I am calling a Web API Post Action passing a JSON parameter. My custom model is as follows:
[Serializable]
public class Model
{
public int? prop1 {get; set;}
public bool prop2 {get; set;}
}
Web API is:
public void Post(Model model)
{
if (model != null && model.prop1 ==5 )
{
// Do something
}
}
The JSON i pass from client is:
var value = {
prop1: 4,
prop2: true
};
And the AJAX call from client is:
.ajax('/api/MyController', {
type: "POST",
contentType: "application/json",
data: JSON.stringify(value),
success:function(data){
alert(Success);
}
});
However, the binding of the model properties never works in the WebAPI action. The "model" param comes back instantiated (it is not null), however all the properties inside are default values and not the values I pass from client. If I remove the [Serializable] attribute from the Model class, it works fine. I cannot remove this attribute since this object gets stored in SQL based session. What are the ways I can get this binding to work without removing the [Serializable] attribute