Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
data sorting via primary key in jquery data table

I have displayed data in view page using data table. I want to display data in descending order according to

public ActionResult Index()
        {
            return View(db.BusinessRegModel.OrderByDescending(v => v.BusinessId).ToList());

        }

BusinessId is primary key.

But in view page, data is not sorted via primary key. I am using jquery data table to display data.

<table id="tblBusinessData" class="table" width="100%" cellspacing="0">
    <thead>
        <tr>
            <th>Edit/Print</th>
            <th>
                @Html.DisplayNameFor(model => model.RegNum)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.RegDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.NameOfFirm)
            </th>
            //code blocks
             </tr>
    </thead>
     <tbody>
        @foreach (var item in Model)
        {
        <tr>
            <td width="20%">
                @Html.ActionLink("Edit", "Edit", new { id = item.BusinessId }, new { @class = "btn btn-warning" })
                @Html.ActionLink("Print", "Details", new { id = item.BusinessId }, new { @class = "btn btn-danger" })
            </td>
            //code blocks
        

But the data is not sorted in descending order via BusinessId key. How can I do this? I need to display data in descending order by BusinessId.

jquery code

<script type="text/javascript">
            $('#tblBusinessData').DataTable();
    </script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Add the column Id to the HTML and hide it via configuration:

$('#tblBusinessData').DataTable({
  "columnDefs": [{
    "targets": [0],
    "visible": false
  }],
  "order": [
    [0, "desc"]
  ]
});
about 4 years ago · Juan Pablo Isaza Report

0

If you are able to set the desired order in your data before you send it to DataTables, you can simply set order: [] to disable the initial sort while still being able to click on the column headers.

$('#tblBusinessData').DataTable({
    order: []
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!