在asp.net mvc3中阅读更多按钮

时间:2013-10-08 18:19:19

标签: javascript jquery ajax database asp.net-mvc-3

我正在使用asp.net mvc3应用程序并且有许多来自数据库的记录。我想先显示10条记录,然后用户可以点击按钮查看下10条记录,依此类推。喜欢facebook壁贴更多记录。如何在我的应用程序中实现此功能?我使用它来获取10条记录,但我想使用更多记录按钮显示所有记录

1 个答案:

答案 0 :(得分:0)

这应该让你去......


假设:

public class PostsViewModel
{
    public IEnumerable<PostViewModel> Posts { get; set; }
}

您的控制器可能如下所示:

public class BlogController
{
    public ActionResult Index()
    {
        PostsViewModel model = new PostsViewModel 
        {
            Posts = postService.GetPosts(resultsPerPage: 10, page: 1)
        };
        return View(model);
    }

    public PartialViewResult More(Int32 page = 1)
    {
        PostsViewModel model = new PostsViewModel 
        {
            Posts = postService.GetPosts(resultsPerPage: 10, page: page)
        };
        return PartialView(model);
    }
}

和观点类似:

<强>〜/查看/博客/ Index.cshtml

@model PostsViewModel
@* Other page content *@
@Html.DisplayFor(x => x.Posts)
<div id="more"></div>
@Ajax.ActionLink("Read More", "More", "Blog", new AjaxOptions {
    InsertionMode = InsertionMode.InsertBefore,
    UpdateTargetId = "more"
})
@* Other page content *@

<强>〜/查看/博客/ More.cshtml

@model PostsViewModel
@Html.DisplayFor(x => x.Posts)

<强>〜/查看/博客/ DisplayTemplates / PostViewModel.cshtml

@model PostViewModel
@* Display post itself *@