我正在尝试从我的控制器返回列表列表为json,
我的控制器代码:
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);
}
和我的ajax电话:
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; }
}
}
当我在返回时放置断点时,model2包含List列表中所需的所有值。但如果我继续他们返回为null。
我遗失了一些但找不到的东西......
并使用AspNet Mvc 5
答案 0 :(得分:2)
您的$.ajax(..)
来电未提供任何成功回拨功能
你可以这样做:
function GetPlans() {
$.ajax({
cache: false,
type: "GET",
url: "/XDetail/GetPpi",
data: {},
dataType: 'json',
success: function(data) {
// use result data
}
});
}
或承诺方法:
function GetPlans() {
$.ajax({
cache: false,
type: "GET",
url: "/XDetail/GetPpi",
data: {},
dataType: 'json'
}).done(function(data){
// use result data
});
}