Web API Model properties are null

不想你离开。 提交于 2021-01-27 05:35:58

问题


My Controller is able to create the model object but all the properties related to model and assigned to null values

Environment : VS 2010, ASP.NET MVC RC latest, jQuery 1.7.1

Following is the Web API Controller code

public class Customer
{
    public string Name { get; set; }
    public string City { get; set; }
}
public class UserController : ApiController
{
    public Customer Post(Customer user)
    {
        return user;
    }
}

Following is the ajax calling code

$.ajax('/api/user',
{
  ContentType: "application/x-www-form-urlencoded; charset=UTF-8",
  dataType: 'json',
  type: 'POST',
  data: JSON.stringify({ "Name": "Scott", "City": "SC" })
});

Controller does create the model "Customer" object but both "Name" and "City" properties are null.

What's wrong here?

I have read many similar issues on this site but could not find the solution.


回答1:


This blog here gives a good idea about how Model Binding differs in ASP.NET Web project and a ASP.NET Web API project.

I was facing a similar issue in the project I was working on and adding a explicit ModelBinding attribute made the property values stick

Requested Data :

var customer  = { Name : "customer Name", City : "custome City" }

$.ajax({ 
   url : ...
   type: ...
   data : customer
});

Request Class:

public class Customer
{
    public string Name { get; set; }
    public string City { get; set; }
}

Controller :

public class UserController : ApiController
{
    [HttpGet]
    public Customer Get([ModelBinder] Customer user)
    {
        // fetch from whereever
    }
}



回答2:


I'm going through the same issue right now. I'm not 100% sure of the answer, but below is my javascript and I've added to the class [DataModel] and to the Properties [DataMember]. That is:

    [DataModel]
    public class Customer
    {
      [DataMember] public string Name { get; set; }
      [DataMember] public string City { get; set; }
    }

And My JavaScript

        $(document).ready(function () {
        // Send an AJAX request
        $.getJSON("api/session/GetAll",
        function (data) {
            // On success, 'data' contains a list of products.
            $.each(data, function (key, val) {

                //debugger;

                // Format the text to display.
                //var str = val.Name + ': $' + val.Price;
                var str = 'abcd';

                // Add a list item for the product.
                $('<li/>', { text: str })
                .appendTo($('#products'));
            });
        });
    });



回答3:


Faced a similar issue and my problem turned out to be invalid JSON in the body of the request. There was a comma missing after one of the fields. So it seems like the default model binder just binds null if there are any syntax errors in the JSON.




回答4:


Next time this happens, ensure that there is no "internal" keyword on the property setter. That is: instead of public string Comment {get; internal set;} use public string Comment {get; set;}

This fixed it for me.



来源:https://stackoverflow.com/questions/14084746/web-api-model-properties-are-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!