代码之家  ›  专栏  ›  技术社区  ›  Jatin Bhatia

如何在DotNet MVC中实现分页

  •  0
  • Jatin Bhatia  · 技术社区  · 7 年前

    我有一个代码,其中我以HTML的表格形式从数据库中获取了一些细节。我想实现相同的分页,但无法实现。

    提前谢谢。

    下面是我的代码 指数 我在对象中获取数据的地方。

    public ActionResult Index(SearchPostedJob objSearchPostedJob)
        {
            Result res = null;
            try
            {
                objSearchPostedJob.page = (objSearchPostedJob.page == null ? 1 : objSearchPostedJob.page);
                ViewBag.SearchPostedJob = objSearchPostedJob;
                res = objPostedJobDAL.GetPostedJobs(ApplicationSession.CurrentUser.RecruiterId);
    
                if (res.Status)
                {
                    ViewBag.Message = "";
                }
                else
                {
                    ViewBag.Message = Common.GetMessage(res.MessageId, "[Master]", "Keyword");
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog(ex);
                throw ex;
            }
            return View(res.Results);
        }
    

    下面是我在HTML上的代码,我用表格格式显示数据和其他一些细节:

    @model IEnumerable<NurseOneStop.SC.PostedJob>
    @{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
    }
        <div class="postjob-form">
    
    @using (Html.BeginForm("Index", "PostedJob", FormMethod.Get))
    {
        <div class="j_search" style="margin-left:3%;">
            <input class="form-control" name="SearchKeyword" id="SearchKeyword" placeholder="Search Keyword" />
        </div>
        <div class="j_search">
            <input id="pinput1" name="JobLocation" class="form-control" type="text" value="@ViewBag.SearchPostedJob.JobLocation" placeholder="Enter a location">
            <input id="Latitude" name="Latitude" type="hidden" value="@ViewBag.SearchPostedJob.Latitude">
            <input id="Longitude" name="Longitude" type="hidden" value="@ViewBag.SearchPostedJob.Longitude">
        </div>
        <div class="j_search">
            <select class="form-control" name="Experience" id="Experience">
                <option value="" selected>Select Experience</option>
                @for (int i = 1; i <= 10; i++)
                {
                    <option value="@i" @(ViewBag.SearchPostedJob.Experience == i ? "selected" : "")>@i</option>
                }
            </select>
        </div>
        <div class="j_search">
            <select class="form-control" name="Salary" id="Salary">
                <option value="" selected>Select Salary</option>
                @for (int i = 1; i <= 10; i++)
                {
                    <option value="@i" @(ViewBag.SearchPostedJob.Salary == i ? "selected" : "")>@i</option>
                }
            </select>
        </div>
    
        <div class="add-btn"><a class="btn btn-primary" href="~/PostedJob/Save?returnUrl=/PostedJob/Index">Add New Opening</a></div>
    
        @*<div class="srch-btn"><button type="submit" class="btn btn-primary">Search</button></div>*@
    }
    </div>
    
    <div>
    
    <table class="table" id="myTable">
        <tr>
    
            <th>
                @Html.DisplayNameFor(model => model.JobTitle)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.JobLocation)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.NumberOfPositions)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ValidFrom)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ValidTo)<br />
            </th>
    
            <th>
                @Html.DisplayNameFor(model => model.JobReference)
            </th>
    
            <th>
                @Html.DisplayNameFor(model => model.Status)
            </th>
            <th>
                Application
            </th>
            <th>
                CloneJob
            </th>
    
            @foreach (var item in Model)
            {
            <tr>
    
                <td>
                    @Html.DisplayFor(modelItem => item.JobTitle)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.JobLocation)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.NumberOfPositions)
                </td>
    
                <td>
                    @Html.DisplayFor(modelItem => item.ValidFrom)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ValidTo)<br />
    
                    @* @Html.ActionLink("Delete Resume", "DeleteResume", new { ResumeId = Model.ResumeId, returnUrl = "/Nurse/ProfileView" }, new { @class = "btnViewJobDetails" })*@
                    @if (!item.IsExtendJob)
                    {
    
    
                        @Html.ActionLink("ExtendJob", "ExtendJob", new { PostedJobId = item.PostedJobId, IsExtendJob = item.IsExtendJob, returnUrl = "/PostedJob/Index" }, new { @class = "btnViewJobDetails" })
                    }
                    else
                    {
                        <span class="pull-right" style="font-weight:bold; color:red;">JobExtended</span>
                    }
    
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.JobReference)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Status)
                </td>
                <td>
                    @Html.ActionLink("Applied(" + item.AppliedCnt + ")", "JobView", new { PostedJobId = item.PostedJobId, returnUrl = "/PostedJob/Index" }, new { @class = "btnViewJobDetails" })
    
                </td>
                <td>
                    @Html.ActionLink("JobClone", "CloneJob", new { PostedJobId = item.PostedJobId, returnUrl = "/PostedJob/Index" }, new { @class = "btnViewJobDetails" })
                </td>
    
            </tr>
            }
    
        </table>
    </div>
    
    
    @section Scripts
     {
        @Scripts.Render("~/bundles/jqueryui")
        @Styles.Render("~/Content/themes/base/css")
        @Scripts.Render("~/bundles/jqueryval")
        <script>
            function initMap() {
                var input = document.getElementById('pinput1');
                var autocomplete = new google.maps.places.Autocomplete(input);
                autocomplete.addListener('place_changed', function () {
                    var place = autocomplete.getPlace();
                    console.log(JSON.stringify(place));
                    if (!place.geometry) {
                        window.alert("Autocomplete's returned place contains no geometry");
                        return;
                    }
                    var latlong = JSON.parse(JSON.stringify(place.geometry.location));
                    document.getElementById('Latitude').value = latlong.lat;
                    document.getElementById('Longitude').value = latlong.lng;
                });
            }
            initMap();
        </script>
    
        <script>
            $(document).ready(function () {
                $("#SearchKeyword").on("keyup", function () {
                    var value = $(this).val().toLowerCase();
                    $("#myTable tr").filter(function () {
                        $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
                    });
                });
            });
        </script>
    
        <script>
            $(document).ready(function () {
                $("#pinput1").on("keyup", function () {
                    var value = $(this).val().toLowerCase();
                    $("#myTable tr").filter(function () {
                        $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
                    });
                });
            });
        </script>
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Linh Dao    7 年前

    按照Microsoft提供的以下指南进行分页: Microsoft pagination tutorial

        2
  •  0
  •   Chaitanya Gadkari    7 年前

    我建议您在Index方法中进行更改,以接受页码和页面大小作为参数,如下所示(并使用查询字符串传递)

    public ActionResult Index(SearchPostedJob objSearchPostedJob, int pageSize, int pageNumber)
    {
        Result res = null;
        try
        {
            objSearchPostedJob.page = (objSearchPostedJob.page == null ? 1 : objSearchPostedJob.page);
            ViewBag.SearchPostedJob = objSearchPostedJob;
            res = objPostedJobDAL.GetPostedJobs(ApplicationSession.CurrentUser.RecruiterId, pageNumber, pageSize); //Changes in DAL are required
    
            if (res.Status)
            {
                ViewBag.Message = "";
            }
            else
            {
                ViewBag.Message = Common.GetMessage(res.MessageId, "[Master]", "Keyword");
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog(ex);
            throw ex;
        }
        return View(res.Results);
    }
    

    然后在DAL中进行更改,以便只提供所需数量的对象。然后使用html/jquery可以创建一个寻呼机,它将为第一、最后、下一和上一页创建链接。

    推荐文章