如何在控制器(MVC Core 2.2)中映射 DataTable 参数?
How to map DataTable parameters in controller (MVC Core 2.2)?
我正在尝试将数据表参数发送到服务器,但是过去在 .NET Framework 中工作的内容似乎在 .NET Core 中不起作用。似乎它只是无法映射主 DTParameters
对象中的对象,因为我得到了 Length
、Draw
、Start
.
的值
DTParameters
class:
public class DTResult<T>
{
public int draw { get; set; }
public int recordsTotal { get; set; }
public int recordsFiltered { get; set; }
public IEnumerable<T> 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 int SearchOption { get; set; }
}
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; }
}
ajax调用:
ajax: {
"type": "GET",
"url": '@Url.Action("GetCompanies", "Company")',
"data": function (data) {
return data;
}
我一直使用 function (data) { return data = JSON.stringify(data); }
,但我必须在此处删除 stringify,以便至少将一些值发送到服务器。
在Startup.cs
中:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
最后,CompanyController
:
public object GetCompanies(DTParameters param)
{
return _companyService.GetCompaniesForDatatable(param, CurrentClient);
}
param
的Search
属性始终为空。
所以我将方法从 GET
更改为 POST
并且成功了。现在我得到了我需要的所有值。
嗯...
我正在尝试将数据表参数发送到服务器,但是过去在 .NET Framework 中工作的内容似乎在 .NET Core 中不起作用。似乎它只是无法映射主 DTParameters
对象中的对象,因为我得到了 Length
、Draw
、Start
.
DTParameters
class:
public class DTResult<T>
{
public int draw { get; set; }
public int recordsTotal { get; set; }
public int recordsFiltered { get; set; }
public IEnumerable<T> 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 int SearchOption { get; set; }
}
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; }
}
ajax调用:
ajax: {
"type": "GET",
"url": '@Url.Action("GetCompanies", "Company")',
"data": function (data) {
return data;
}
我一直使用 function (data) { return data = JSON.stringify(data); }
,但我必须在此处删除 stringify,以便至少将一些值发送到服务器。
在Startup.cs
中:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
最后,CompanyController
:
public object GetCompanies(DTParameters param)
{
return _companyService.GetCompaniesForDatatable(param, CurrentClient);
}
param
的Search
属性始终为空。
所以我将方法从 GET
更改为 POST
并且成功了。现在我得到了我需要的所有值。
嗯...