为什么从c#webapi填充Jquery数据表时会收到错误消息?

时间:2017-08-22 19:54:22

标签: javascript c# jquery asp.net-web-api datatables

我一直在webforms中使用带有c#的WebApi 2来返回UserNames和Passwords的列表,因此可以在Jquery Datatable中使用但是它不起作用并抛出错误:

请求第0行第0列的未知参数“0”。

API:

namespace WebApiHimHer.Controllers
{
    public class UsersController : ApiController
    {
        [HttpGet]
        public DTResult GetData([FromUri]DTParameters v)
        {

            //List<string[]> s = new List<string[]>();

            List<basicoperations> s = new List<basicoperations>();

            basicoperations bo= new basicoperations();

            s = bo.getUsers(10, 0, 0, "asc", "");

            var jsonSerialiser = new JavaScriptSerializer();
            var json = jsonSerialiser.Serialize(s);

            DTResult r = new DTResult();
            r.draw = 1;
            r.recordsFiltered = 25;
            r.recordsTotal = 25;
            r.data = s;

            return r;
        }
    }

    public class DTResult
    {
        public int draw { get; set; }
        public int recordsTotal { get; set; }
        public int recordsFiltered { get; set; }
        public List<basicoperations> data { get; set; }
    }
    public abstract class DTRow
    {
        public virtual string DT_RowId
        {
            get { return null; }
        }
        public virtual string DT_RowClass
        {
            get { return null; }
        }
        public virtual object DT_RowData
        {
            get { return null; }
        }
    }
    public class DTParameters
    {
        public int draw { get; set; }
        public DTColumn[] columns { get; set; }
        public DTOrder[] order { get; set; }
        public int start { get; set; }
        public int length { get; set; }
        public DTSearch search { get; set; }
        public string sortOrder
        {
            get
            {
                return columns != null && order != null && order.Length > 0
                    ? (columns[order[0].Column].Data + (order[0].Dir == DTOrderDir.DESC ? " " + order[0].Dir : string.Empty))
                    : null;
            }
        }
    }
    public class DTColumn
    {
        public string Data { get; set; }
        public string Name { get; set; }
        public bool Searchable { get; set; }
        public bool Orderable { get; set; }
        public DTSearch Search { get; set; }
    }
    public class DTOrder
    {
        public int Column { get; set; }
        public DTOrderDir Dir { get; set; }
    }
    public enum DTOrderDir
    {
        ASC,
        DESC
    }
    public class DTSearch
    {
        public string Value { get; set; }
        public bool Regex { get; set; }
    }

}

Jquery的:

function show()
            {                               

                                $('#example').DataTable( {
//                                  "processing": true,
                                    "serverSide": true,
                                    "contentType": 'application/json; charset=utf-8',
                                    "ajax": "http://localhost:28071/Users"


                                } );
            }   

来自Webapi的回复:

{ “画”:1, “recordsTotal”:25, “recordsFiltered”:25 “数据”:[{ “用户名”: “Hunain”, “密码”: “123”},{”用户名 “:” Hunain”, “密码”: “123”},{ “用户名”: “ravaid”, “密码”: “123”},{ “用户名”: “瓦利德”, “密码”: “123” },{“UserName”:“Jim Carrey”,“密码”:“123”},{“UserName”:“da”,“密码”:“asd”},{“UserName”:“hunain”,“密码“:” 321 “},{” 用户名 “:” SD “ ”密码“: ”ASD“},{ ”用户名“: ”bekhudi“, ”密码“: ”123“},{ ”用户名“:” HK ”, “密码”: “ASD”}]}

2 个答案:

答案 0 :(得分:0)

我添加了列

   $('#example').DataTable({
        //                                  "processing": true,
        "serverSide": true,
        "contentType": 'application/json; charset=utf-8',
        "ajax": "http://localhost:28071/Users",
        columns:[{data:"UserName"}, { data:"Password"}]

    });

答案 1 :(得分:0)

完成,

这是因为我正在返回一个对象数组而不是一个字符串数组。