我如何处理<> f__AnonymousType0`2 [System.Int32,System.String] []?

时间:2015-08-10 06:47:27

标签: c# ajax asp.net-mvc

我想访问从方法返回的List。

但是我得到了错误

  

未捕获的TypeError:无法读取未定义的属性“length”,

当我调试我的代码时,我在变量结果中得到了返回值,如<> f__AnonymousType0`2 [System.Int32,System.String] [] ,那么如何处理和使用return我从方法返回的值?

$('#DTAttendance tbody').on('click', 'tr', function () {
     var aData = oTable.fnGetData(this);
     var lectureid = aData[0];
     $.ajax({
         type: "POST",
         url: '@Url.Action("GetStudentList", "Attendance")',
         contentType: "application/json; charser=utf-8",
         data: JSON.stringify({ 'lectureid': lectureid }),
         success: function (result) {
            if (result != 0) {
              var dynhtml = "";
              $.each(result.allrecord, function (i, item) {
                dynhtml += "<tr><td>" + item.sid + "<td><td>" + item.sname + "<td></tr>"
            });
            dynhtml = "<table>" + dynhtml + "</table>";
            $("#dttable").html(dynhtml);
           }
          else {
            alert("Some error");
               }
           }
    });
  });
}

GetStudentList函数

 [HttpPost]
 public object GetStudentList(Int32 lectureid)
   {
    try
      {
        object allrecord = _IAcademy_Repository.GetStudentRecordAccordingAttendanceWise(lectureid);
        return allrecord;
      }
    catch (Exception ex)
      {
        Console.WriteLine("\nMessage ---\n{0}", ex.Message);
        return 0;
      }
    }

1 个答案:

答案 0 :(得分:1)

将其放在控制器端

[HttpPost]
        public object GetStudentList(Int32 lectureid)
        {
            try
            {
                object allrecord = _IAcademy_Repository.GetStudentRecordAccordingAttendanceWise(lectureid);
                return Json(allrecord);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\nMessage ---\n{0}", ex.Message);

                return 0;

            }
        }


// And You Should Place 



$.each(result, function (i, item) {
                            dynhtml += "<tr><td>" + item.sid + "<td><td>" + item.sname + "<td></tr>"
                        });