0

In asp.net-core controller I sort descending model collection by BirthDate property and return it to the view. While debugging prepared order is respected, but before showing view table changes sort direction to ascending again.

Is there a way to set default sort order from ascending to descending for table column just by applying Bootstrap classes in Razor view?

<table id="table" class="table table-striped table-bordered" style="width:100%">
            <thead>
                <tr>
                    <th>Birth date</th>
                    <th>Name</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var person in Model)
                {
                    <tr>
                        <td>@person.BirthDate</td>
                        <td>@person.Name</td>

                    </tr>
                }
            </tbody>
        </table>

What is the simplest way to accomplish that?

David762
  • 455
  • 4
  • 20
  • Bootstrap does not support it. But you can use some plugins for this. http://tutsme-webdesign.info/bootstrap-3-sortable-table/, https://stackoverflow.com/questions/31888566/bootstrap-how-to-sort-table-columns – Tomato32 May 30 '17 at 07:44

1 Answers1

1

I attached a script placed in a separate .js file to the view page.

$(document).ready(function() {
    $('#table').DataTable( {
        "order": [[ 0, "desc" ]]
    } );
} );

It works ;-)

David762
  • 455
  • 4
  • 20