【问题标题】:MVC 4 Using Paged List in a partial ViewMVC 4 在部分视图中使用分页列表
【发布时间】:2015-09-29 08:05:32
【问题描述】:

我正在尝试在部分视图中实现 PagedList。

描述视图设置。我有Controller A with ViewA。这是父视图,有自己的模型。然后我有Controller B with PartialViewB 并且也有自己的模型。然后我在ViewA 中有一个Div,将用于显示PartialViewB。我可以在点击按钮后加载PartialViewB,然后在再次点击按钮后隐藏视图。 PartialViewB 内是 PagedList。点击下一页按钮会加载下一页,但会将其加载到自己的页面中,而不是像以前那样在 ViewA 中加载。

我可以根据需要加载更多代码,但现在这里是寻呼机

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

    @Html.PagedListPager(Model, page => Url.Action("ViewComments",
    new { courseID = @ViewBag.courseID, page }), 
    new PagedListRenderOptions { MaximumPageNumbersToDisplay = 5, DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded, 
        DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded })

::编辑::

父视图

<div class="Comments">
    <input type="button" id="View" class="CommentsButton" value="View Comments"/>
    <input type="hidden" id="Hidden" value="false" />
</div>
<div id="Comments">
</div>

局部视图

@model PagedList.IPagedList<QIEducationWebApp.Models.CourseComment>
@using PagedList.Mvc;

@{
    ViewBag.Title = "Comments";
}

<h2>Comments!</h2>

<table>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CommentDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UserName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CommentText)
        </td>
    </tr>
}

</table>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<appSettings>
  <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings>

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

    @Html.PagedListPager(Model, page => Url.Action("ViewComments",
    new { courseID = @ViewBag.courseID, page }),
            PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(
                new PagedListRenderOptions { MaximumPageNumbersToDisplay = 5, DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded, 
                DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded },
                new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "Comments" }))

BundleConfig.cs

public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css", "~/Content/PagedList.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));
        }
    }

【问题讨论】:

  • 一般会有多少数据被发送到这个局部视图?你真的需要服务器端分页吗?
  • 因此它将是存储在数据库中的 cmets 列表。当然,它们所附加的用户、日期和评论。所以不确定哪个最好。
  • 我没有看到对 jquery.unobtrusive-ajax.js 的引用。转到 App_Start > BundleConfig.cs 并查看是否有包含此引用的包。还要确保 unobtrusive-ajax 文件甚至在您的解决方案中。
  • jquery.unobtrusive-ajax.js 在 scripts 目录下,但不在 BundleConfig.cs 中。
  • 我撒谎了,也许是的。

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-partialview pagedlist


【解决方案1】:

看看这个:Related SO question

这将使用不显眼的 ajax 为您进行替换。您只需要在最后处理 fetch 和 skip 并将新的局部视图与模型一起发回。

@Html.PagedListPager(Model, page => Url.Action("ViewComments", page }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing( new AjaxOptions(){  HttpMethod = "GET", UpdateTargetId = "partialContainerYouNeedToReplace"}))

确保您在执行此操作时在您的页面上引用了不显眼的 js。它带有开箱即用的 MVC,您应该只需要引用该捆绑包。

希望这会有所帮助。

【讨论】:

  • 目前仍在做同样的事情。生病添加更新的寻呼机。而且不知道它是否重要,但是将被引用的 div 在父视图中,所以我可以在部分视图中的寻呼机中引用它吗?
  • UpdateTargetId 应该是您页面上已经存在的 div。如果您只是将 @Html.Partial(...) 包装在具有适当 id 的 div 中,那应该没问题。
  • 不知道您是否愿意在类似主题的其他方面帮助我。我有办法发布 cmets。在有人发表评论后,我只想刷新回第一页,显示新评论。你能指出我正确的方向吗?
  • 与您的第一个问题的解决方案类似,您可以使用 Ajax.BeginForm(...){ //html 标记和输入 }。您需要确保在您想要此功能的页面上引用了不显眼的 ajax js。这提供了各种重载,包括返回的结果列表部分的 updateElement ID。查看这些文档:msdn.microsoft.com/en-us/library/…
  • 搞错了。父视图持有视图 cmets 的局部视图,然后该局部视图具有另一个显示表单以发布 cmets 的视图。因此,当您发表评论时,父视图需要重新加载包含 cmets 视图的 div
猜你喜欢
  • 2016-06-03
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
  • 2013-09-20
  • 1970-01-01
相关资源
最近更新 更多