Aniket Narvankar

Aniket Narvankar

  • 562
  • 2.1k
  • 605k

Paging in Asp.Net Core

Mar 3 2024 6:29 PM

I am implementing paging in asp.net core mvc 6.0
here is my code on controller

using X.PagedList;
public IActionResult Index(int? page)
        {
            Employee emp = new Employee();
            emp.EmpModel = _repository.GetAllEmployee().ToPagedList(page ?? 1,3);
            return View(emp);
             
        }

here is code on view
 

@model IPagedList<Asp.NetCoreApp.Model.Employee>
@using X.PagedList.Mvc.Core;
@using X.PagedList;
@using X.PagedList.Web.Common;

 
@{
    Layout = null;
}
@using (Html.BeginForm("Index","Home",FormMethod.Post,new{@class="form-horizontal"}))
{
    <div class="form-group">
        @Html.LabelFor(model => model.First().Department, new {@class="control-label col-sm-2"})
        <div class="col-sm-6">
            @Html.DropDownList("Department",new List<SelectListItem>
        {
        new SelectListItem { Text = "IT", Value = "IT"},
        new SelectListItem { Text = "HR", Value = "HR"},
        new SelectListItem { Text = "Payroll", Value = "Payroll"}
        },"Select Department",new{@class="form-control"})
            @Html.ValidationMessageFor(model => model.First().Department, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.First().EmpName, new { @class = "control-label col-sm-2" })
        <div class="col-sm-6">
            @Html.TextBoxFor(model =>model.First().EmpName, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.First().EmpName, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.First().EmailId, new { @class = "control-label col-sm-2" })
        <div class="col-sm-6">
            @Html.TextBoxFor(model => model.First().EmailId, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.First().EmailId, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-6">
            <input type="submit" id="btnSubmit" value="Submit" class="btn btn-primary" />
        </div>
    </div>
}
@{
    if(Model.First().EmpModel != null && Model.First().EmpModel.Count > 0)
    {
        <table class="table table-striped" border="1">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Department</th>
                    <th>Edit</th>
                    <th>Delete</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var emp in Model.First().EmpModel)
                {
                    using (Html.BeginForm("Delete","Home",new { id = emp.Id },FormMethod.Post))
                    {
                        <tr>
                            <td>@emp.EmpName</td>
                            <td>@emp.EmailId</td>
                            <td>@emp.Department</td>
                            <td>
                                @Html.ActionLink("Edit", "Edit", new { id = emp.Id },new { @class = "btn btn-primary" })</td>
                            <td>
                                <input type="submit" value="Delete" onclick="return confirm('Are you sure you want to delete record with ID = @emp.Id');" class="btn btn-danger"/>
                            </td>
                        </tr>
                    }               
                }
               
            </tbody>
        </table>
        @Html.PagedListPager(Model, page => Url.Action("Index", new { page }), new PagedListRenderOptions { DisplayLinkToFirstPage = PagedListDisplayMode.Always, DisplayLinkToLastPage = PagedListDisplayMode.Always })
    }
}
Please let me know what needs to be done
i am stuck on this


Answers (4)