1

I am sending JSON objects from client but when I get my model all properties bind with default value.

I try some ways for example I got data as string but no results my expected. So how can i solve this?

Web API Config

public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    var json = config.Formatters.JsonFormatter;
    json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
    config.Formatters.Remove(config.Formatters.XmlFormatter);
}

Model

[Serializable]
public class WorkerPuantaj
{
    public int workT { get; set; }
    public int PDay { get; set; }
    public int PMonth { get; set; }
    public int PYear { get; set; }
    public int worker { get; set; }
}

Client Request

$.ajax({
       url: "/api/BuildingApi/AddPuantajItems",
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       type: 'POST',
       data: JSON.stringify(jsonData),
       success: function (data) {
           alert("oldu");
       },
       error: function (a, b, c) {
           alert("olmadı");
       }
   });

JSON Being Sent

"[{"workT":1,"PDay":20,"PMonth":4,"PYear":2014,"worker":3},{"workT":2,"PDay":21,‌​"PMonth":4,"PYear":2014,"worker":3},{"workT":3,"PDay":22,"PMonth":4,"PYear":2014,‌​"worker":3}]

Debugger Display

4
  • Right now it's impossible to answer this question. What does the actual data that you're sending to the server look like? What does your server-side method look like? Commented Apr 14, 2014 at 20:26
  • You can see in question so this i sent "[{"workT":1,"PDay":20,"PMonth":4,"PYear":2014,"worker":3},{"workT":2,"PDay":21,"PMonth":4,"PYear":2014,"worker":3},{"workT":3,"PDay":22,"PMonth":4,"PYear":2014,"worker":3}]" Commented Apr 14, 2014 at 20:27
  • I ran into a problem similar to this in deserializing JSON once. Try declaring the method argument for AddPuantajItems as List<WorkerPuantaj> instead of WorkerPuantaj[]. Commented Apr 14, 2014 at 20:46
  • Firstly thank you for edit my question.So it didn't change, same result. Commented Apr 14, 2014 at 20:48

1 Answer 1

1

Since you are sending data as JSON array, in the method "AddPuantajItems" you should have your parameter as WorkerPuantaj[] but it is not the reason of the problem you have.

If you use Serializeable attribute on an MVC Web API model, it can't deserialize incoming JSON data into your model type. I don't know the reason but it fixed the issue for my test case.

Remove [Serializable] keyword and let me know if it has been fixed.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.