Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am trying to fetch information through an MVC controller by sending a JSON object to it as parameter.

The controller method looks like this:

public ActionResult GetLatestInfoLogs(InfoLogUserJson invoker, InfoLogUserJson affected) {
    // code
}

I have a server side model which looks like this:

public class InfoLogUserJson {
    public int id { get;set; }
    public int personId { get;set;}
    public int customerId { get;set; }
    public int rootUserId { get;set; }
    public string ip { get;set; }
    public bool unknown { get;set; }
}

and I have a client-side script which looks like this:

var InfoLogUser = function () {
    this.id = ko.observable(0);
    this.personId = ko.observable(0);
    this.rootUserId = ko.observable(0);
    this.customerId = ko.observable(0);
    this.unknown = ko.observable(false);
    this.ip = ko.observable(null);
}

InfoLogUser.prototype = (function() {
     return {
          toJSON: function() {
            return {
                personId: this.getPersonId(),
                rootUserId: this.getRootUserId(),
                customerId: this.getCustomerId(),
                id: this.getId(),
                unknown: this.getUnknown(),
                ip: this.getIp()
             }
         }
     }
}());

In a javascript view model I am trying to do this:

            var infoLogUser = new InfoLogUser();
            infoLogUser.personId(1234);

            $.ajax({
                url: '/Whatever/GetLatestInfoLogs',
                data: {
                    invoker: JSON.stringify(infoLogUser.toJSON()),
                    affected: null
                },
                dataType: 'application/json; charset: utf-8',
                type: 'GET',
                success: function(_infoLogs) {
                    alert('yay');
                }
            });

In my network log I get the following: Query String Parameters:

invoker:{"personId":1234,"rootUserId":0,"customerId":0,"id":0,"unknown":false,"ip":null} affected:

However, when it hits the GetLatestInfoLogs method in the MVC controller, the invoker parameter is always null. If I remove the JSON.stringify from the ajax request the invoker parameter isn't null, but no value has been set in it.

I can't really figure out what's going on, so hopefully any of you guys might know what's going on? :)

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Try this

$.ajax({
        url: '/Whatever/GetLatestInfoLogs',
        type: 'POST',
        data: {
            invoker: JSON.stringify(infoLogUser),
            affected: null
        },
        contentType: 'application/json',

        success: function(_infoLogs) {
            alert('yay');
        }
    });

Updated

var data = {invoker: infoLogUser, affected: null};
$.ajax({
            url: '/Whatever/GetLatestInfoLogs',
            type: 'POST',
            data: JSON.stringify(data),
            contentType: 'application/json',

            success: function(_infoLogs) {
                alert('yay');
            }
        });
share|improve this answer
    
When I try to change to POST as type I get 500 internal error. –  Ekenstein Jan 26 at 12:18
1  
@Ekenstein Try the updated one. –  ozil Jan 26 at 12:29
    
This worked if I did: var data = { invoker: infoLogUser, affected: null}; .... data: JSON.stringify(data), .... Thanks! :D –  Ekenstein Jan 26 at 12:32
1  
@Ekenstein I made a mistake, it should be like this data: JSON.stringify(data) , you picked it :D –  ozil Jan 26 at 12:59

You don't need to create your own to .toJSON() method.

Simply do this:

invoker: JSON.stringify(infoLogUser)

You also need to make sure that you set the content type i.e.

contentType: 'application/json'

I also noticed that your data type should be:

dataType: 'json'

The entire call would become:

    var infoLogUser = new InfoLogUser();
    infoLogUser.personId(1234);

    $.ajax({
        url: '/Whatever/GetLatestInfoLogs',
        data: {
            invoker: JSON.stringify(infoLogUser),
            affected: null
        },
        dataType: 'json',
        contentType: 'application/json',
        type: 'GET',
        success: function(_infoLogs) {
            alert('yay');
        }
    });

Update To be valid json you need to change invoker & affected to strings i.e.

 data: {
         "invoker": JSON.stringify(infoLogUser),
         "affected": null
        }
share|improve this answer
    
Sadly this didn't work, the invoker parameter in the MVC controller is still null after this change. –  Ekenstein Jan 26 at 10:49
    
@Ekenstein did you change the datatype? –  hutchonoid Jan 26 at 10:50
    
Yepp :) Still nothing –  Ekenstein Jan 26 at 10:51
    
@Ekenstein See update. :D –  hutchonoid Jan 26 at 10:58
    
@hutchonoid Adding quotes around the keys shouldn't matter since invoker and affected aren't reserved words. –  heymega Jan 26 at 11:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.