【问题标题】:mvc pagedlist date loses value on second pagemvc pagedlist 日期在第二页上失去价值
【发布时间】:2015-03-31 08:24:18
【问题描述】:

我有一个索引页面,上面有各种过滤选项,都包含在 PagedList 中。除了日期之外,它们似乎都工作正常。

当我第一次按日期过滤时,它们工作正常,但是当我点击底部的页码时,我的日期的搜索条件消失了。

我可以看到我可以在分页列表中传递搜索词,我可以看到这个日期命中了我的控制器并且过滤发生了,但是 ViewBag.filterStartDate 和 ViewBag.filterEndDate 只是没有绑定回我的文本框某种原因。

Index.cshtml:

@using PagedList.Mvc
@model PagedList.IPagedList<Job>

@using (Html.BeginForm("Index", "Jobs", FormMethod.Get, new { @class = "form-inline", role = "form" }))
{
    <div class="panel panel-default">
        <!-- Default panel contents -->
        <div class="panel-heading">Filter Search Results</div>
        <div class="panel-body">

            <ul class="list-group">
                <li class="list-group-item">
                    @Html.Label("By Id: ", new { @class = "col-md-4 control-label" })
                    @Html.TextBox("filterId", ViewBag.filterId as string, new { @class = "form-control" })
                </li>
                <li class="list-group-item">
                    @Html.Label("By Address Line 1: ", new { @class = "col-md-4 control-label" })
                    @Html.TextBox("filterAddress1", ViewBag.filterAddress1 as string, new { @class = "form-control" })
                </li>
                <li class="list-group-item">
                    @Html.Label("By Username: ", new { @class = "col-md-4 control-label" })
                    @Html.TextBox("filterUsername", ViewBag.filterUsername as string, new { @class = "form-control" })
                </li>
                <li class="list-group-item">
                    @Html.Label("By Contract: ", new { @class = "col-md-4 control-label" })
                    @Html.DropDownList("filterContract", null, "-- Select One --",
                        new { @class = "form-control" })
                </li>
                <li class="list-group-item">
                    @Html.Label("Date Created Start: ", new { @class = "col-md-4 control-label" })
                    @Html.TextBox("filterStartDate", ViewBag.filterStartDate as string, new { @class = "form-control date", type = "date" })
                </li>
                <li class="list-group-item">
                    @Html.Label("Date Created End: ", new { @class = "col-md-4 control-label" })
                    @Html.TextBox("filterFinishDate", ViewBag.filterFinishDate as string, new { @class = "form-control date", type = "date" })
                </li>
                <li class="list-group-item">
                    @Html.Label("By App: ", new { @class = "col-md-4 control-label" })
                    @Html.DropDownList("filterApp", null, "-- Select One --",
                        new { @class = "form-control" })
                </li>
            </ul>

            <input type="submit" value="Apply Filter" class="btn btn-default" />

        </div>

        <div id="items" style="padding: 15px;">
            @Html.Partial("Jobs", Model)
        </div>

    </div>

}

Jobs.cshtml:

@using System.Web.UI.WebControls
@using PagedList.Mvc
@model PagedList.IPagedList<Job>

@Html.ActionLink("Create New", "Create", null, new { @style = "float: left" })

@Html.ActionLink("Import Jobs", "Import", null, new { @style = "padding-left: 15px" })

@Html.ValidationSummary(true)  

<table class="table">
    <tr>
        <th class="mobileview">
            @Html.DisplayName("JobId")
        </th>
        <th>
            @Html.DisplayName("Description")
        </th>
        <th class="mobileview">
            @Html.DisplayName("Address")
        </th>
        <th class="mobileview">
            @Html.DisplayName("Priority")
        </th>
        <th>
            @Html.DisplayName("Date Created")
        </th>
        <th>
            @Html.DisplayName("Username")
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr class="formrow">
            <td class="mobileview">
                @Html.DisplayFor(modelItem => item.JobId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td class="mobileview">
                @Html.DisplayFor(modelItem => item.Address1)
            </td>
            <td class="mobileview">
                @Html.DisplayFor(modelItem => item.Priority)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DateCreated)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.User.UserName)
            </td>
            <td class="mobileview">
                @Html.ActionLink("View Job", "Details", new { id = item.JobId })
            </td>
            <td class="mobileview">
                @if (item.Data != null)
                {
                    @Html.ActionLink("View Data", "Details", "AppForms", new { id = item.Data.Id }, null)
                }
                else
                {
                    @Html.DisplayFor(modelItem => item.Status)
                }
            </td>
        </tr>
    }

</table>

<p>Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount</p>

@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, filterAddress1 = ViewBag.filterAddress1, filterId = ViewBag.filterId, filterUsername = ViewBag.filterUsername, filterStartDate = ViewBag.filterStartDate, filterFinishDate = ViewBag.filterFinishDate, filterApp = ViewBag.selectedApp, filterContract = ViewBag.selectedContract }), PagedListRenderOptions.Classic)

JobsController.cs:

public ActionResult Index(string sortOrder, string currentFilter, string filterId, string filterAddress1, int? page, String filterApp, String filterContract, DateTime? filterStartDate, DateTime? filterFinishDate, String filterUsername)
{
    //...Other filtering

    //Filter by date
    if (filterStartDate != null)
    {
        jobs = jobs.Where(x => x.DateCreated >= filterStartDate);
        //ViewBag.filterStartDate = filterStartDate.Value.Year + "-" + filterStartDate.Value.Month + "-" + filterStartDate.Value.Day;
        ViewBag.filterStartDate = filterStartDate;
    }

    if (filterFinishDate != null)
    {
        //Make sure we're checking to the end of the end date (ie 01/01/2015 23:59:59)
        filterFinishDate = filterFinishDate.Value.AddHours(23).AddMinutes(59).AddSeconds(59);
        jobs = jobs.Where(x => x.DateCreated <= filterFinishDate);
        //ViewBag.filterFinishDate = filterFinishDate.Value.Year + "-" + filterFinishDate.Value.Month + "-" + filterFinishDate.Value.Day;
        ViewBag.filterFinishDate = filterFinishDate;
    }

    int pageSize = int.Parse(ConfigurationManager.AppSettings["MaxPageItemCount"]);
    int pageNumber = page ?? 1;

    return this.View(jobs.ToPagedList(pageNumber, pageSize));
}

【问题讨论】:

  • 从索引操作返回作业时,它们的排序是否正确?
  • 目前我实际上并没有使用任何排序。当前未使用 sortOrder。相反,我只是按 DateCreated DESC 对它们进行排序,但我选择省略那行代码,因为我认为这对我遇到的问题并不重要。

标签: c# asp.net-mvc razor pagedlist


【解决方案1】:

@Html.TextBox() 可以从 ModelState 或 ViewData 中提取值。

尝试像这样手动构建日期输入的 html:

<input type="date" name="filterStartDate" id="filterStartDate" class="form-control date" value="@ViewBag.filterStartDate.ToString("yyyy-MM-dd")"/>

【讨论】:

  • 此解决方案已正常工作。感谢您分享您令人难以置信的知识和专业知识。
【解决方案2】:

通过手动创建日期输入解决了这个问题,而不是依靠 Razor 为我做这件事!

                @if (ViewBag.filterStartDate != null)
                {
                    <input type="date" name="filterStartDate" id="filterStartDate" value="@ViewBag.filterStartDate.ToString("yyyy-MM-dd")" />
                }
                else
                {
                    <input type="date" name="filterStartDate" id="filterStartDate" value="" />
                }

【讨论】:

    猜你喜欢
    • 2013-09-14
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    相关资源
    最近更新 更多