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

5
  • Share your Json method? Commented Oct 16, 2017 at 22:51
  • Edited the question for your comments Commented Oct 16, 2017 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. Alternatively make XDetail be a simple DTO with no parent type. Commented Oct 16, 2017 at 23:16
  • @dbc it is just public abstract partial class, not marked with [DataContract]. Commented Oct 16, 2017 at 23:21
  • Actually It was working fine, just I didn't see it. Commented Oct 16, 2017 at 23:33

1 Answer 1

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
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.