Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jason Espin 368 posts 1335 karma points
    Jan 14, 2015 @ 11:48
    Jason Espin
    0

    Passing JSON object to a surface controller

    Hi all,

    I'm having some issues posting a JSON object to my surface controller using ajax. I have checked in my Firebug Net window and the JSON string is posted in the correct format however, when it gets to the method within my surface controller it is effectively interpreted as null. What am I doing wrong here as I have followed one of the examples from the Umbraco documentation almost to the letter yet 'data' always appears to be null when it reaches the GetAvailabilty method.

    Javascript

    $('#productavailability').click(function () {
            var data = {};
            data.packageCode = $(this).data('id');
    
            $.ajax({
                url: '/umbraco/surface/product/GetAvailability',
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify(data),
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    $('#availabilityResult').html(data.Availability);
                }
            })
        });
    

    Surface Controller

    public class ProductController : SurfaceController
        {
            // Declare security credentials globally for use with all methods
            Guid guid = new Guid(ConfigurationManager.AppSettings["axumcompanyguid"]);
            String companyName = ConfigurationManager.AppSettings["axumcompanyname"];
    
            // Setup managed user header
            ManagedUserHeader managedUserHeader = new ManagedUserHeader();
    
            // Setup compression soap header
            CompressionSoapHeader compressionSoapHeader = new CompressionSoapHeader();
    
            // Setup Web Service
            TailorMadeWebServiceSoapClient ws = new TailorMadeWebServiceSoapClient();
    
            [HttpPost]
            public ActionResult GetAvailability(string data)
            {
                Package p = JsonConvert.DeserializeObject<Package>(data);
                int test = p.packageCode;
                managedUserHeader.CompanyName = companyName;
                managedUserHeader.RegistrationKey = guid;
                compressionSoapHeader.CompressionMethodUsed = CompressionMethods.None;
    
                ProductAvailDataGroup[] ws_availability = null;
                ws_availability = ws.GetProductAvailability(ref compressionSoapHeader, ref managedUserHeader, p.packageCode, DateTime.Today, DateTime.Today.AddDays(28));
    
                return null;
            }
        }
    

    Any help or pointers would be greatly appreciated.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 14, 2015 @ 12:06
    Jeroen Breuer
    100

    You could try to create a c# object based on your json: http://json2csharp.com/

    If it's called AvailabilityObject you could try to get the data like this:

    public ActionResult GetAvailability(AvailabilityObject data)

    The model binding of MVC should try to convert the json to the generated object.

    If your json has a value called packageCode you could also try to get it like this:

    public ActionResult GetAvailability(string packageCode)

    Jeroen

  • Jason Espin 368 posts 1335 karma points
    Jan 14, 2015 @ 12:15
    Jason Espin
    0

    Hi Jeroen,

    Thanks for your response. Funnily enough I just worked all that out for myself. It appears that for each parameter name in my method there needs to be a matching json key for the method to work correctly.

Please Sign in or register to post replies

Write your reply to:

Draft