Web.Api Model Binding for [Serializable] class is not working

时间:2015-07-13 21:11:04

标签: c# asp.net-mvc asp.net-web-api

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?

1 个答案:

答案 0 :(得分:0)

无论Serializable属性如何,您的模型绑定都可以正常工作,请确保发送数据的代码正确无误。以下代码在DTO上有和没有Serializable属性的情况下运行良好。

$(function() {

    var data = { userId : "Shyju" };

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

});

和值控制器中的服务器代码

public string Post(ApiClass model)
{
    return "Received" + model.UserId;
}