asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了加载ASP.Net MVC JSONResult jQuery DataTables大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让DataTables(http://datatables.net)使用ASP.Net MVC控制器返回的JsonResult.我继续收到一个“DataTables警告(表id =’example’):从数据源为第0行请求的未知参数’0’错误,根据文档意味着它找不到列.

返回JsonResult的控制器中的代码如下所示:

public JsonResult LoadPhonenumbers()
    {
        List<Phonenumber> phonenumbers = new List<Phonenumber>();
        Phonenumber num1 = new Phonenumber { number = "555 123 4567",Description = "George" };
        Phonenumber num2 = new Phonenumber { number = "555 765 4321",Description = "Kevin" };
        Phonenumber num3 = new Phonenumber { number = "555 555 4781",Description = "Sam" };

        phonenumbers.Add(num1);
        phonenumbers.Add(num2);
        phonenumbers.Add(num3);

        return Json(phonenumbers,JsonrequestBehavior.AllowGet);
    }
@H_508_2@phonenumber只是一个普通的C#类,具有2个属性number和Description.

检索和加载数据的javascript如下所示:

<script>
$(document).ready(function () {
    $('#example').dataTable({
        "bProcessing": true,"sAjaxsource": '/Account/LoadPhonenumbers/',"sAjaxDataProp": ""
    });
});
</script>

而Html看起来像:

<table celLPADding="0" cellspacing="0" border="0" class="display" id="example">
<thead>
    <tr>
        <th>
            number
        </th>
        <th>
            Description
        </th>
    </tr>
</thead>
<tbody>
</tbody>
<tfoot>
</tfoot>
</table>

我故意将sAjaxDataProp设置为空字符串,以便DataTables不会查找aaData.即使我在控制器中显式设置aaData:

return Json(new { aaData = phonenumbers });

我仍然收到错误.有什么建议吗

谢谢!

解决方法

以下作品对我来说非常棒:
$(function () {
    $('#example').dataTable({
        bProcessing: true,sAjaxsource: '@Url.Action("LoadPhonenumbers","Home")'
    });
});

我已经删除了sAjaxDataProp属性.

与此数据源:

public ActionResult LoadPhonenumbers()
{
    return Json(new
    {
        aaData = new[] 
        {
            new [] { "Trident","Internet Explorer 4.0","Win 95+","4","X" },new [] { "Gecko","Firefox 1.5","Win 98+ / OSX.2+","1.8","A" },new [] { "Webkit","iPod Touch / iPhone","iPod","420.1","A" }
        }
    },JsonrequestBehavior.AllowGet);
}

并为您的例子与手机简单地:

public ActionResult LoadPhonenumbers()
{
    var phonenumbers = new List<Phonenumber>(new[] 
    {
        new Phonenumber { number = "555 123 4567",Description = "George" },new Phonenumber { number = "555 765 4321",Description = "Kevin" },new Phonenumber { number = "555 555 4781",Description = "Sam" }
    });

    return Json(new
    {
        aaData = phonenumbers.SELEct(x => new[] { x.number,x.Description })
    },JsonrequestBehavior.AllowGet);
}

大佬总结

以上是大佬教程为你收集整理的加载ASP.Net MVC JSONResult jQuery DataTables全部内容,希望文章能够帮你解决加载ASP.Net MVC JSONResult jQuery DataTables所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。