将Datatables POST数组转换为C#模型
我正在使用datatables向asp mvc发送数据。net结构如下将Datatables POST数组转换为C#模型,c#,datatables,C#,Datatables,我正在使用datatables向asp mvc发送数据。net结构如下 draw:1 columns[0][data]:first_name columns[0][name]: columns[0][searchable]:true columns[0][orderable]:true columns[0][search][value]: columns[0][search][regex]:false columns[1][data]:last_name columns[1][name]: co
draw:1
columns[0][data]:first_name
columns[0][name]:
columns[0][searchable]:true
columns[0][orderable]:true
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:last_name
columns[1][name]:
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false
order[0][column]:0
order[0][dir]:asc
start:0
length:10
search[value]:
search[regex]:false
如何将这些数据拾取/绑定到c#类,c#类是Action方法的参数,实际上是开始、长度、绘制地图,但是
栏目[n][nnn]
给空
我的c#模范班是
公共类数据表模型
{
publicstringdraw{get;set;}
公共ICollection列{get;set;}
//公共字符串[]顺序{get;set;}
public int start{get;set;}
公共整数长度{get;set;}
公共列表顺序{get;set;}
public int recordsTotal{get;set;}
公共int记录筛选器{get;set;}
公共数据表模型()
{
columns=新列表();
订单=新列表();
}
}
公共类列
{
公共字符串数据{get;set;}
公共字符串名称{get;set;}
公共字符串可搜索{get;set;}
公共字符串可排序对象{get;set;}
公共列表搜索{get;set;}
}
公共类搜索
{
公共布尔正则表达式{get;set;}
公共字符串值{get;set;}
}
公共阶级秩序
{
公共字符串列{get;set;}
公共字符串dir{get;set;}
}
谢谢不,这不是我使用的最新版本,它不发布iSortCol\n。答案是我没有包括“contentType”:“application/json”。这就是我在mvc操作参数中选择空列列表的原因。一旦我把它放在mvc地图所有领域
var table = $('#example').dataTable({
"processing": true,
"serverSide": true,
'ajax': {
'type': 'POST',
'contentType': "application/json",
'url': '/TestDistributor/GetAllForGrid',
'data': function (d) {
console.log(JSON.stringify(d));
return JSON.stringify(d);
}
},
"columns": [
{ 'data': 'DistributorCode' },
{ 'data': 'DistributorName' },
{ 'data': 'AreaCode' },
{ 'data': 'TownCode' },
{ 'data': 'CityCode' },
]
});
以下是课程
public class JQDTParams
{
public int draw { get; set; }
public int start { get; set; }
public int length { get; set; }
public JQDTColumnSearch /*Dictionary<string, string>*/ search { get; set; }
public List<JQDTColumnOrder/*Dictionary<string, string>*/> order { get; set; }
public List<JQDTColumn/*Dictionary<string, string>*/> columns { get; set; }
}
public enum JQDTColumnOrderDirection
{
asc, desc
}
public class JQDTColumnOrder
{
public int column { get; set; }
public JQDTColumnOrderDirection dir { get; set; }
}
public class JQDTColumnSearch
{
public string value { get; set; }
public string regex { get; set; }
}
public class JQDTColumn
{
public string data { get; set; }
public string name { get; set; }
public Boolean searchable { get; set; }
public Boolean orderable { get; set; }
public JQDTColumnSearch search { get; set; }
}
公共类JQDTRAM
{
公共int draw{get;set;}
public int start{get;set;}
公共整数长度{get;set;}
公共JQDTColumnSearch/*Dictionary*/search{get;set;}
公共列表顺序{get;set;}
公共列表列{get;set;}
}
公共枚举JQDTColumnOrderDirection
{
asc,desc
}
公共类JQDTColumnOrder
{
公共int列{get;set;}
公共JQDTColumnOrderDirection目录{get;set;}
}
公共类JQDTColumnSearch
{
公共字符串值{get;set;}
公共字符串正则表达式{get;set;}
}
公共类JQDTColumn
{
公共字符串数据{get;set;}
公共字符串名称{get;set;}
公共布尔可搜索{get;set;}
公共布尔序表{get;set;}
公共JQDTColumnSearch{get;set;}
}
和用法
HTML
主键
马尔卡斯
模型一
主键
马尔卡斯
模型一
$(文档).ready(函数(){
$('aractipiListesi')。数据表({
“处理”:对,
“服务器端”:正确,
“过滤器”:正确,
“页面长度”:8,
“栏目”:[
{
“名称”:“ID”,
“可订购”:false
},
{
“名称”:“MARKAADI”,
“可订购”:真
},
{
“名称”:“蒂帕迪”,
“可订购”:真
},
{
“名称”:“秒”,
“可订购”:false
}
],
“ajax”:
{
url:“@url.Action”(“AracTipiAra”,“Common”,新{area=”“})”,
类型:“职位”
},
“columnDefs”:
[
{
“render”:函数(数据、类型、行){return AracTipiListesiTableDropDownToggle(数据、类型、行);},
“目标”:[3]
},
{
“可见”:假,
“目标”:[0]
}
],
“语言”:DTConstants.language
});
var aractipi_Filtreneckekbasliklar=['Markası','Modeli'];
//设置-向每个页脚单元格添加文本输入
$('#aractipiListesi thead.filter input th')。每个(函数(){
var title=$(this.text();
if(title!=''&$.inArray(title,aractipi_filtereleneckekbasliklar)>=0){
$(this.html(“”);
}
});
//数据表
var table=$('#aractipiListesi').DataTable();
//应用搜索
table.columns().every(函数(){
var=这个;
$('input',this.footer()).on('keyup change',function(){
if(that.search()!==此.value){
that.search(this.value).draw();
}
});
});
});
控制器
public JsonResult AracTipiAra(JQDTParams参数)
{
使用(var db=new mbossenties())
{
var q=从db中的x开始。VW_ARACMARKATIPI选择x;
var nonfilteredcount=q.Count();
//滤器
//-------------------------------------------------------------------
foreach(参数列中的变量项)
{
var filterText=item.search.value;
如果(!String.IsNullOrEmpty(filterText))
{
filterText=filterText.ToLower();
开关(项目名称)
{
“MARKAADI”案:
q=q.在哪里(
x=>
x、 MARKAADI.ToLower()包含(filterText)
);
打破
ca
public class JQDTParams
{
public int draw { get; set; }
public int start { get; set; }
public int length { get; set; }
public JQDTColumnSearch /*Dictionary<string, string>*/ search { get; set; }
public List<JQDTColumnOrder/*Dictionary<string, string>*/> order { get; set; }
public List<JQDTColumn/*Dictionary<string, string>*/> columns { get; set; }
}
public enum JQDTColumnOrderDirection
{
asc, desc
}
public class JQDTColumnOrder
{
public int column { get; set; }
public JQDTColumnOrderDirection dir { get; set; }
}
public class JQDTColumnSearch
{
public string value { get; set; }
public string regex { get; set; }
}
public class JQDTColumn
{
public string data { get; set; }
public string name { get; set; }
public Boolean searchable { get; set; }
public Boolean orderable { get; set; }
public JQDTColumnSearch search { get; set; }
}
<div>
<table id="aractipiListesi" class="display" cellspacing="0" width="100%">
<thead>
<tr class="filter-input">
<th>PK</th>
<th>Markası</th>
<th>Modeli</th>
<th></th>
</tr>
<tr>
<th>PK</th>
<th>Markası</th>
<th>Modeli</th>
<th></th>
</tr>
</thead>
</table>
<script type="text/javascript">
$(document).ready(function () {
$('#aractipiListesi').DataTable({
"processing": true,
"serverSide": true,
"filter": true,
"pageLength": 8,
"columns": [
{
"name": "ID",
"orderable": false
},
{
"name": "MARKAADI",
"orderable": true
},
{
"name": "TIPADI",
"orderable": true
},
{
"name": "SEC",
"orderable": false
}
],
"ajax":
{
url: "@Url.Action("AracTipiAra", "Common", new { area = "" })",
type: "post"
},
"columnDefs":
[
{
"render": function (data, type, row) { return AracTipiListesiTableDropDownToggle(data, type, row); },
"targets": [3]
},
{
"visible": false,
"targets": [0]
}
],
"language": DTConstants.Language
});
var aractipi_filtrelenecekBasliklar = ['Markası', 'Modeli'];
// Setup - add a text input to each footer cell
$('#aractipiListesi thead .filter-input th').each(function () {
var title = $(this).text();
if (title != '' && $.inArray(title, aractipi_filtrelenecekBasliklar) >= 0) {
$(this).html('<input type="text" placeholder="' + title + ' Ara" />');
}
});
// DataTable
var table = $('#aractipiListesi').DataTable();
// Apply the search
table.columns().every(function () {
var that = this;
$('input', this.footer()).on('keyup change', function () {
if (that.search() !== this.value) {
that.search(this.value).draw();
}
});
});
});
</script>
</div>
public JsonResult AracTipiAra(JQDTParams param)
{
using (var db = new MBOSSEntities())
{
var q = from x in db.VW_ARACMARKATIPI select x;
var nonfilteredcount = q.Count();
//filter
//-------------------------------------------------------------------
foreach (var item in param.columns)
{
var filterText = item.search.value;
if (!String.IsNullOrEmpty(filterText))
{
filterText = filterText.ToLower();
switch (item.name)
{
case "MARKAADI":
q = q.Where(
x =>
x.MARKAADI.ToLower().Contains(filterText)
);
break;
case "TIPADI":
q = q.Where(
x =>
x.TIPADI.ToLower().Contains(filterText)
);
break;
}
}
}
//order
//-------------------------------------------------------------------
foreach (var item in param.order)
{
string orderingFunction = "MARKAADI";
switch (item.column)
{
case 1: orderingFunction = "MARKAADI";
break;
case 2: orderingFunction = "TIPADI";
break;
}
q = OrderClass.OrderBy<VW_ARACMARKATIPI>(q, orderingFunction, item.dir.GetStringValue());
}
//result
//-------------------------------------------------------------------
var filteredCount = q.Count();
q = q.Skip(param.start).Take(param.length);
var data = q.ToList()
.Select(r => new[] {
r.ARACMARKAPK.ToString(),
r.MARKAADI,
r.TIPADI,
}
);
return Json(new
{
draw = param.draw,
recordsTotal = nonfilteredcount,
recordsFiltered = filteredCount,
data = data
}, JsonRequestBehavior.AllowGet);
}
}