【问题标题】:How to Bind controller to multidimensional query string parameter in .NET Core/5如何在 .NET Core/5 中将控制器绑定到多维查询字符串参数
【发布时间】:2021-04-14 04:37:36
【问题描述】:

我有一个 Web 客户端,它使用 jQuery 数据表进行服务器端分页和排序。它通过查询字符串以特定方式发送参数:

这里是发送到服务器的订单参数示例:

&order[0][column]=1&order[0][dir]=asc&start=0&length=10&search[value]=&search[regex]=false&_=1618229879540

我很难在 ApiController 类中绑定这些参数。

控制器代码:

        [HttpGet]
        public async Task<ActionResult<DataTable<Audit>>> GetAudits([FromQuery]DataTableParams dataTableParams)
        {
            var audits = await _auditRepository.GetAuditsAsync(dataTableParams);

            return Ok(audits);
        }

参数类:

    public class DataTableParams
    {
        public int Draw { get; set; }
        public int Start { get; set; }
        public int Length { get; set; } = 30;
        public string Dir { get; set; }
        public IEnumerable<DtOrder> Order { get; set;}
    }

    public class DtOrder
    {
        /// <summary>
        /// Column to which ordering should be applied.
        /// This is an index reference to the columns array of information that is also submitted to the server.
        /// </summary>
        public int Column { get; set; }

        /// <summary>
        /// Ordering direction for this column.
        /// It will be dt-string asc or dt-string desc to indicate ascending ordering or descending ordering, respectively.
        /// </summary>
        public string Dir { get; set; }
    }

我做错了什么?我试过 DtOrder[] 而不是 IEnumerable。在每种情况下,框架都无法绑定到 order 参数,lenght/count 为 0。

【问题讨论】:

    标签: asp.net-core asp.net5


    【解决方案1】:

    对于那些正在寻找这个确切东西的人。

    1. Datatables 以特定格式的查询字符串发送数据。
    2. 似乎没有一种简单的方法来绑定需要为此创建自定义绑定器的数据。

    有一个 NuGet 包专门处理帮助格式化和响应数据表请求。 DataTables.AspNet.AspNetCore 是一个包,它有一个活页夹以及能够绑定到数据库查询字符串以及格式化数据表响应所需的一切。

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 2018-05-08
      • 2022-09-24
      相关资源
      最近更新 更多