简体   繁体   English

如何在mvc视图中按日期列出数据顺序

[英]how to list data order by date in mvc view

Iam using mvc, i want to retreive data in index view order by date. 我使用mvc,我希望按日期顺序检索索引视图中的数据。

var cn = from a in db.table1 
         join b in db.table2 on a.Country equals b.CountryCode
         join c in db.table3 on a.DealerId equals b.CompID  
         order by a.date desc select a;

return View(cn.ToList());

without datatables it is working fine, but datatables not allowing to sort by date. 没有数据表它工作正常,但数据表不允许按日期排序。 it is only displaying the data by primary key value order Any help will be greatly appreciated. 它只按主键值顺序显示数据任何帮助将不胜感激。

您可以使用linq orderby,例如:

.OrderByDescending(x => x.Date);
$(document).ready(function() {
            $('#id').dataTable( {
                "aaSorting": [[ 2, "desc" ]]  //2 - number of column
            } );
        } );

if you dont want to display the date column through which you are sorting then use 如果您不想显示要排序的日期列,请使用

"aaSorting": [[12, "desc"]],
        "aoColumnDefs": [{ "bVisible": false, "aTargets": [12] }],

upto this level it will sort only columns which are in datatable so u need to use linq query 在此级别,它将仅对数据表中的列进行排序,因此您需要使用linq查询

return view(cn.OrderByDescending.ToList());

Look at this example of usage. 看看这个用法示例。
Set "aaSorting" attribute to sort by date. 设置“aaSorting”属性以按日期排序。

$(document).ready(function() {
            $('#youTableId').dataTable( {
                "aaSorting": [[ 2, "desc" ]]  //2 - number of column
            } );
        } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM