Home > database >  ascending order of data from database by Id
ascending order of data from database by Id

Time:07-27

I am trying to learn asp.net-core, and I am stuck on trying to retieve the data from database in ascending order of database Id, my code is below but not sure why its not doing it, it does acccessnding order by page but I want all the data in ascending order by database Id, any pointers wold be great...

    @model IEnumerable<Citrus.Models.Customers>

@{
    ViewData["Title"] = "Index";

    Pager pager = new Pager();
    int pageNo = 0;

    if (ViewBag.Pager !=null)

    {
        pager = ViewBag.Pager;
        pageNo = pager.CurrentPage;
    }


}


<form asp-controller="Customers" asp-action="Index">
    <p>


        <div >
            <div >
                <div >

                    <div >
                        <label  for="searchString"></label>
                        <div >
                            <input  type="text" name="searchString" placeholder="Search by company name" />
                            <span  name="searchString"><button ><i ></i> Search</button></span>
                            @*<span asp-validation-for="searchString"></span>*@
                            <a  asp-action="Create"><i ></i> Customer</></a>
                        </div>

                    </div>
                </div>
            </div>
        </div>

    </p>

</form>

<table >
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Company)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Title)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Surname)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Tel)
            </th>

            <th></th>
        </tr>
    </thead>
    <tbody>                                
        @foreach (var item in Model.OrderByDescending(i => i.Id))
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Company)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.FirstName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Surname)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Tel)
                </td>

                <td>
                    <a asp-action="Edit" asp-route-id="@item.Id"><i ></i></a> |
                    <a asp-action="Details" asp-route-id="@item.Id"><i ></i></a> |
                    <a asp-action="Delete" asp-route-id="@item.Id"><i ></i></a>
                </td>
            </tr>
        }
    </tbody>
</table>

<div >
    @if (pager.TotalPages > 0)

    {
        <ul >

@if (pager.CurrentPage > 1)
{ 

    <li >
        <a  asp-controller="Customers" asp-action="Index" asp-route-pg="1">First</a>
        </li>
        <li>
            <a  asp-controller="Customers" asp-action="Index" asp-route-pg="@(pager.CurrentPage -1)">Previous</a>

        </li>
}
















        @for (var pge = pager.StartPage; pge <= pager.EndPage; pge  )

        {
            <li active" : "")">
                <a  asp-controller="Customers" asp-action="Index" asp-route-pg="@pge"> @pge</a>
            </li> 
        }


            @if (pager.CurrentPage < pager.TotalPages)

            {
                <li >
                    <a  asp-controller="Customers" asp-action="Index" asp-route-pg="@(pager.CurrentPage   1)">Next</a>
                </li>
                <li>
                    <a  asp-controller="Customers" asp-action="Index" asp-route-pg="@(pager.TotalPages)">Last</a>
                </li>

            }

        </ul>

    }




</div>

Many Thank John

CodePudding user response:

You need to use OrderBy in backend,use OrderBy firstly,and then get the current page data,for example:

List<Citrus.Models.Customers> data = _context.Customers
    .OrderBy(c => c.Id)
    .Skip(position).Take(pageSize)
    .ToList();

For details,you can refer to the official doc.

  • Related