[asp.net] json 데이터를 mvc controller 로 보낼때

Posted by RAY.D
2015. 4. 27. 13:56 Web/ASP.NET MVC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.





Sending json to asp.net mvc controller does make objects, but doesn't fill properties



http://stackoverflow.com/questions/20307983/sending-json-to-asp-net-mvc-controller-does-make-objects-but-doesnt-fill-prope




So I want to post my jsondata that I've received from the facebook javascript SDK on my asp.NET webserver. The problem that I'm getting is that the facebookfriend-objects are being created (facebookfriendlist is populated), but aren't being filled (id = null, name = null for each object in the facebookfriendlist).

Json format

{"data":[{"name":"friend x","id":"integer here"},{"name":"friend y","id":"integer here"},...],"paging":{"next":"tokenXXX"}}

My jquery post (json is filled, I've printed the content into a div to check)

FB.api('/me/friends', function (response) {
        $("#friends").html(JSON.stringify(response));
        pb.progressbar("value", 66);
        $.ajax({
            url: '/MapMe/Retrieve',
            type: 'POST',
            data: response,
            accept: 'application/json',
            success: successFunc,
            error: errorFunc
        });

        function successFunc(data, status) {
            pb.progressbar("value", 100);
            //window.location.href = './App';
        }

        function errorFunc() {
            alert("Failed to get Data")
        }
    });

My controller:

[HttpPost]
    public ActionResult Retrieve(FacebookFriendlist friends)
    {
    System.Diagnostics.Debug.WriteLine(friends);
    return Json(new {status = true});
    }

My objectclasses

 public class FacebookFriendlist
    {
        public FacebookFriendlist()
        {
            data = new List<Facebookfriend>();
        }
        public IList<Facebookfriend> data { get; set; }
        public string paging { get; set; }
    }

    public class Facebookfriend
    {
        public string name { get; set; }
        public string id { get; set; }

    }









For starters, you have accept in the jQuery .ajax() function, but it should be accepts, like this:

$.ajax({
    url: '/MapMe/Retrieve',
    type: 'POST',
    data: response,
    accepts: 'application/json',
    success: successFunc,
    error: errorFunc
});

Per jQuery .ajax() documentation:

accepts (default: depends on DataType)

The content type sent in the request header that tells the server what kind of response it will accept in return. If the accepts setting needs modification, it is recommended to do so once in the $.ajaxSetup() method.