Как реализовать нумерацию страниц в DotNet MVC - PullRequest
0 голосов
/ 25 сентября 2018

У меня есть код, в котором я получаю несколько деталей из базы данных в виде таблицы в 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 ]

0 голосов
/ 25 сентября 2018

Я бы предложил вам внести изменения в метод индекса, чтобы принять номер страницы и размер страницы в качестве параметров, как показано ниже (и передать их с помощью строки запроса)

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 вы можете создать пейджер, который будет создавать ссылку для 1-й, последней, следующей и предыдущей страниц.

0 голосов
/ 25 сентября 2018

Выполните нумерацию страниц в соответствии с указаниями Microsoft: Учебник по пагинации Microsoft

...