0

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

AIyer
  • 106
  • 1
  • 7
  • 1
    Try to [configure the JsonSerializer to ignore the `Serializable` attribute](http://stackoverflow.com/a/22486064/997668) – Michael Sep 11 '14 at 16:12
  • 1
    possible duplicate of [.NET WebAPI Serialization k\_BackingField Nastiness](http://stackoverflow.com/questions/12334382/net-webapi-serialization-k-backingfield-nastiness) – Filip W Sep 11 '14 at 18:14

1 Answers1

0

Remove [Serializable] from your Model and it should do it. Not sure why, but it's not working when the class is marked as Serializable.

Himal Patel
  • 387
  • 4
  • 19