Ajax错误 - jquery datatable [英] Ajax error - jquery datatable
问题描述
我在我的应用程序中使用
你应该返回Json(结果)。构建列表后,只有在抛出异常时才返回一个空数据。将您的方法结果更改为:
return Json(result);
}
catch(Exception ex)
{
return Json(new {error = ex.Message});
}
}
I am using jquery datatable in my application.
When I am trying to bind the json response from the server I am getting the following message in the browser
DataTables warning:table id=DataTables_Table_0-Ajax error.For more information about this error please seee http://datatables.net/tn/7
and in the console of the browser as
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
.
When I dived deeped into the console i got the following exception A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.CenterCode_16F8807C95C21FEFA99B4700E38D8ACB4A88C8E560B5640BD5E1FA148C99CCA5'.
And I dont know what this means??
Josn is returning successfully without any error?How can I solve this issue?
JsonResult Function
public JsonResult GetDataTable(string finYear)
{
try
{
Common _cmn = new Common();
List<RegistraionVM.RegDataTable> _dTableReg = new List<RegistraionVM.RegDataTable>();
_dTableReg = _db.StudentRegistrations
.AsEnumerable()
.Where(r => _centerCodeIds.Contains(r.StudentWalkInn.CenterCode.Id)
&& (r.TransactionDate.Value.Date >= _startFinDate && r.TransactionDate.Value.Date <= _endFinDate))
.Select(r => new RegistraionVM.RegDataTable
{
Centre = r.StudentWalkInn.CenterCode.CentreCode,
CourseFee = r.TotalCourseFee.Value,
Discount = r.Discount.Value,
CurrEmpId = Int32.Parse(Session["LoggedUserId"].ToString()),
WalkInn = r.StudentWalkInn,
Receipt = r.StudentReceipts.Where(rc => rc.Status == false).FirstOrDefault(),
RegDate = r.TransactionDate,
RegistrationID = r.Id,
SalesPerson = r.StudentWalkInn.CROCount == (int)EnumClass.CROCount.ONE ?
r.StudentWalkInn.Employee1.Name :
r.StudentWalkInn.Employee1.Name + "," + r.StudentWalkInn.Employee2.Name,
StudentName = r.StudentWalkInn.CandidateName,
SoftwareUsed = string.Join(",", r.StudentRegistrationCourses
.SelectMany(c => c.MultiCourse.MultiCourseDetails
.Select(mc => mc.Course.Name))),
IsSalesIndividual = _currentRole == (int)EnumClass.Role.SALESINDIVIDUAL ? true : false
}).OrderByDescending(r => r.RegistrationID).ToList();
return Json(new { data = _dTableReg }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { data = "" }, JsonRequestBehavior.AllowGet);
}
}
RegDataTable
public class RegDataTable
{
public int RegistrationID { get; set; }
public DateTime? RegDate { get; set; }
public string Centre { get; set; }
public string SalesPerson { get; set; }
public string StudentName { get; set; }
public int Discount { get; set; }
public int CourseFee { get; set; }
public string SoftwareUsed { get; set; }
public StudentReceipt Receipt { get; set; }
public bool IsSalesIndividual { get; set; }
public StudentWalkInn WalkInn { get; set; }
public int CurrEmpId { get; set; }
public int? NextDueAmount
{
get { return Receipt == null ? 0 : Receipt.Total; }
}
public string NextDueDate
{
get { return Receipt == null ? "" : Receipt.DueDate.Value.ToString("dd/MM/yyyy"); }
}
public string MobileNo
{
get
{
if (IsSalesIndividual)
{
if ((CurrEmpId == WalkInn.CRO1ID) || (CurrEmpId == WalkInn.CRO2ID))
{
return WalkInn.MobileNo;
}
else
{
return "-";
}
}
else
{
return WalkInn.MobileNo;
}
}
}
}
html
<table class="table table-bordered table-striped dTable" data-url="@Url.Action("GetDataTable", "Registration")">
<thead>
<tr>
<th>RegDate</th>
<th>Centre </th>
<th>Sales Person </th>
<th>Student Name</th>
<th>Mobile</th>
<th style="width:200px">S/W Used</th>
<th>Discount</th>
<th>CourseFee</th>
<th>Next DueDetails</th>
<th style="display:none">NextDueAmount</th>
<th ></th>
</tr>
</thead>
</table>
Calling DataTable from javascript
table = $(".dTable").dataTable({
...
...,
columns: [
{ "data": "RegDate" },
{ "data": "Centre" },
{ "data": "SalesPerson" },
{ "data": "StudentName" },
{ "data": "MobileNo" },
{ "data": "SoftwareUsed" },
{ "data": "Discount" },
{ "data": "CourseFee" },
{ "data": "NextDueDate" },
{ "data": "NextDueAmount" },
{ "data": "RegistrationID" }
],
//Defining checkbox in columns
"aoColumnDefs": [
{
"targets": [0],
"render": function (data, type, full, meta) {
var date = new Date(parseInt(data.substr(6)));
var month = date.getMonth() + 1;
return date.getDate() + '/' + month + '/' + date.getFullYear()
}
},
{
"targets": [8],
"bSortable": false,
"render": function (data, type, row) {
if (row.NextDueAmount != 0) {
return data + ',' + row.NextDueAmount
}
else {
return "FULL PAID"
}
}
},
],
});
JsonResponse
You should return Json(result). After you have built the list, you are only returning an empty data if exception thrown. Change your method result to:
return Json(result);
}
catch (Exception ex)
{
return Json(new { error = ex.Message });
}
}
这篇关于Ajax错误 - jquery datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!