咨询电话:186 7916 6165 咨询电话:186 7916 6165 (微信同号)    在线QQ:181796286
NEWS BLOG ·
学无止境
关注开优网络 关注前沿
MVC使用下拉列表
没有了

MVC在内容页中使用分页

发表日期:2019-08-21    文章编辑:    浏览次数:275    标签:

控制器:

public ActionResult test(int? id, int pageIndex = 1)  //传入文章id,设置pageindex为第一页
{
    Model.Article model = new DAL.ArticleDAL().GetModel(int.Parse(id.ToString()));  //获取文章的Model

    if (pageIndex < 1)
    {
        pageIndex = 1;
    }
    int pageSize = 2;

    string cond = " article_id=" + id + " ";           //条件

    int total = new DAL.Article_commentDAL().CalcCount(cond);  //总页数

    List<Model.Article_comment> list = new DAL.Article_commentDAL().GetListArray("*", "createdate desc,id desc", pageSize, pageIndex, cond); //续取列表
    PagedList<Model.Article_comment> commentlist = new PagedList<Model.Article_comment>(list, pageIndex, pageSize, total);  //分页
    ViewBag.commentlist = commentlist;  //把分页返回到动态视图里

    return View(model);  //返回文章Model
}

视图:

@{ 
    Layout = "~/Views/Shared/TopDown.cshtml";
    Webdiyer.WebControls.Mvc.PagedList<XiaobinManage.Model.Article_comment> commentlist = ViewBag.commentlist;    //分页
}
@using Webdiyer.WebControls.Mvc;  //引用分页控件
@model XiaobinManage.Model.Article

<div class="con_div">
 <div class="con_title">@Model.title</div>  //文章正文内容
</div>
<div class="con_body">
    @Html.Raw(Model.body)
</div>

<div class="con_comment_text">
    <div class="con_comment_text_title">在线评论<span>共有<em>72条</em>评论网友评论</span></div>
    <form action="#">
        <div class="con_comment_text_input_1">
            <div class="avatar-box">姓名:</div>
            <input class="comment_input" id="Text2" type="text" />
            <div class="avatar-box">部门:</div>
            <input class="comment_input" id="Text3" type="text" />
            <div class="clear"></div>
        </div>
        <div class="con_comment_text_input_2">
            <div class="avatar-box">评论内容:</div>
            <textarea id="TextArea1" class="comment_editor" cols="20" rows="2"></textarea>
            <div class="clear"></div>
        </div>
        <div class="con_comment_text_btn">
            <input id="Button1" class="subcon" type="button" value="button" />
        </div>
        <div class="clear"></div>
    </form>
</div>

<ul id="commentList" class="list-box">
    @foreach (XiaobinManage.Model.Article_comment item in ViewBag.commentlist)  //内容页中的分页
    {
        <li>
            <div class="avatar-box">
                <img src="images/o2oxc_icon03.jpg">
            </div>
            <div class="inner-box">
                <div class="info">
                    <span>@item.user_name</span>
                    <span>@item.createdate.ToString("yyyy-MM-dd")</span>
                </div>
                <p>@Html.Raw(item.content)</p>
            </div>
        </li>
    }
</ul>
<div class="clear"></div>
<div class="pagesDiv">
    <span style="float: left;">共 @commentlist.TotalItemCount 条记录,第 @commentlist.CurrentPageIndex 页/共 @commentlist.TotalPageCount 页</span>
    @Html.Pager(commentlist, new PagerOptions { PageIndexParameterName = "pageIndex", CssClass = "pages", NumericPagerItemCount = 5, ShowMorePagerItems = false, CurrentPagerItemTemplate = "<span class=\"cpb\">{0}</span>", DisabledPagerItemTemplate = "<a disabled=\"disabled\">{0}</a>" })
</div>