3

I have a class like;

[Serializable] 
public class ApiClass
{        
    public string UserId { get; set; }
    public DateTime CreateTime { get; set; }        
}

And in my web api controller method is like;

    public Guid Post(ApiClass apiClass)
    {
         // do some stuff with parameter.
    }

when I send object as json request, for some reason, if I use [Serializable] attribute for my class, it does not resolved. But if I remove it I can get values from parameter.

What would be the reason?

Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158

1 Answers1

0

Your Model binding should work fine irrespective of the Serializable attribute, Make sure your code which sends the data is correct. The below code worked fine with and without Serializable attribute on the DTO.

$(function() {

    var data = { userId : "Shyju" };

    $.post("api/Values", data, function(response) {
        document.write(response);
    });

});

and the server code in Values controller

public string Post(ApiClass model)
{
    return "Received" + model.UserId;
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • 1
    I am sending identical serialized json body for both cases. And when I add Serializable attribute, it does not fill properties of the parameter. Reverse scenario works though. – Teoman shipahi Jul 13 '15 at 21:40