0

I am trying to return list of list as json from my controller,

my controller code:

using Newtonsoft.Json;

[HttpGet]
public JsonResult GetPpi()
{
    var customer = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer;
    var psg = _xDetailService.GetXDetailbyCustomerId(customer.Id);

    var model2 = new List<List<XDetail>>();

    foreach (var pr in psg)
    {               
        var plan = _xDetailService.GetXDetailbyId(pr.Id);
        var model = new List<XDetail>();
        foreach (var x in plan)
        {
            model.Add(new XDetail
            {
                Id = x.Id,
                XNo = x.XNo,
                XName = x.XName,
                XSurname = x.XSurname
            });
        }
        model2.Add(model);
   }

   return Json(model2, JsonRequestBehavior.AllowGet);
}

and my ajax call:

function GetPlans() {
    $.ajax({
        cache: false,
        type: "GET",
        url: "/XDetail/GetPpi",
        data: {},
        dataType: 'json',

    });
}

XDetail:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nop.Core.Domain.UnExpected
{
    public partial class XDetail: BaseEntity
    {
        public int? XNo { get; set; }

        public string XName { get; set; }

        public string XSurname { get; set; }

        public double? XDec { get; set; }

        public int? XMaster_Id { get; set; }

        public virtual XMaster XMaster { get; set; }
    }
}

When I put a break point at return model2 contains all the values I needed in List of List. But if I continue they return as null.

I am missing something but couldn't find what...

and also using AspNet Mvc 5

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Share your `Json` method? – Alex McMillan Oct 16 '17 at 22:51
  • Edited the question for your comments –  Oct 16 '17 at 23:06
  • What is `BaseEntity`? If it is marked with `[DataContract]` then you will need to annotate your derived class with data contract attributes also. That's because AspNet Mvc 5 uses Json.NET for serialization, and Json.NET requires this as explained in [caliburn.micro serialization issue when implementing PropertyChangedBase](https://stackoverflow.com/a/29203876/3744182). Alternatively make `XDetail` be a simple DTO with no parent type. – dbc Oct 16 '17 at 23:16
  • @dbc it is just public abstract partial class, not marked with [DataContract]. –  Oct 16 '17 at 23:21
  • Actually It was working fine, just I didn't see it. –  Oct 16 '17 at 23:33

1 Answers1

2

Your $.ajax(..) call does not provide any success callback function You can do it like that:

function GetPlans() {
    $.ajax({
        cache: false,
        type: "GET",
        url: "/XDetail/GetPpi",
        data: {},
        dataType: 'json',
        success: function(data) {
           // use result data
        }

    });
}

or with promise approach:

function GetPlans() {
    $.ajax({
        cache: false,
        type: "GET",
        url: "/XDetail/GetPpi",
        data: {},
        dataType: 'json'
    }).done(function(data){
        // use result data
    });
}
jgasiorowski
  • 1,033
  • 10
  • 20
  • thank you for the answer but my code was already working fine. I just didn't see it. I will accept the answer anyway since it is my fault not to see it :) Ty. –  Oct 16 '17 at 23:34