學用MVC4做網站五:5.2我的文章


文章管理這一塊,按照左側導航這一塊向下寫

image

到了“我的文章”這一塊。

先還是打開【ArticleController】,添加public ActionResult UserOwn(int id = 0, int page = 1)

這里的id是指欄目id,可以顯示自己發布的指定欄目的文章,默認為0顯示說有欄目文章,page是頁號默認為1。

這里也沒什么內容主要是調用

學用MVC4做網站四:公共模型CommonModelRepository的List函數。

/// <summary>
        /// 我的文章
        /// </summary>
        /// <param name="id">欄目id</param>
        /// <param name="page">頁號</param>
        [UserAuthorize]
        public ActionResult UserOwn(int id = 0, int page = 1)
        {
            int _pageSize = 20;
            int _cOrder = 0;
            Category _c = null;
            cModelRsy = new CommonModelRepository();
            PagerData<CommonModel> _aData;
            if (id > 0)
            {
                var _cRsy = new CategoryRepository();
                _c =_cRsy.Find(id);
                if (_c != null)
                {
                    _pageSize = (int)_c.PageSize;
                    _cOrder = (int)_c.ContentOrder;
                }
            }
            _aData = cModelRsy.List(id, false, "Article", UserController.UserName, page, _pageSize, _cOrder);
            if (_c != null)
            {
                _aData.Config.RecordName = _c.RecordName;
                _aData.Config.RecordUnit = _c.RecordUnit;
            }
            return View(_aData);
        }

點右鍵添加強類型視圖。模型類為PagerData<Ninesky.Models.CommonModel>(在添加模型類里沒有這個選項,當時寫分頁控件時是想把分頁控件獨立出來,把PagerData類寫在了Mvc空間里了,這里在添加視圖時什么都不選,在添加完的視圖文件頂部寫上@model PagerData<Ninesky.Models.CommonModel>就行)

image

視圖文件也很簡單,上不是一個表格,@foreach循環添加表格內容,底部添加Html.Pager分頁。

@model PagerData<Ninesky.Models.CommonModel>
@{
    ViewBag.Title = "我的文章";
    Layout = "~/Views/Shared/_User.cshtml";
}

<div class="workspace">
    <div class="inside">
        <div class="notebar">
            <img alt="" src="~/Content/Default/User/Images/Icon/Article_16.png" />您現在的位置: 文章管理
        </div>
        <div>
            <table class="modelitems_table">
                <tr>
                    <th>ID</th>
                    <th>欄目</th>
                    <th>標題</th>
                    <th>發表者</th>
                    <th>發布時間</th>
                    <th>狀態</th>
                    <th>點擊</th>
                    <th colspan="2">操作</th>
                </tr>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>@item.CommonModelId</td>
                        <td>[@Html.ActionLink(item.Category.Name,"UserOwn",new {id=item.CategoryId})]</td>
                        <td class="title">@item.Title</td>
                        <td>@item.Inputer</td>
                        <td>@item.ReleaseDate</td>
                        <td>@Ninesky.Models.CommonModel.ContentStatus.FirstOrDefault(c => c.Value == item.Status.ToString()).Text</td>
                        <td>@item.Hits</td>
                        <td>@Html.ActionLink("修改","UserEdit",new {id = item.CommonModelId})</td>
                        <td><a>刪除</a></td>
                    </tr>
                }
            </table>
            @Html.Pager(this.ViewContext.RouteData.Values,Model.Config,"pager","pager")
        </div>
    </div>
</div>
<div class="left">@Html.Partial("PartialUserNavMenus")<br />
</div>
<div class="clear"></div>

F5看下效果

image

代碼見:

學用MVC4做網站五:文章

============================

最后說的就是:感謝兄弟姐們對我的支持,我是一名業余愛好者,畢業這么多年了工作也沒從事軟件這塊了,人都墮落了。業余時間又想學點東西了。看到你們隊我的關注和留言覺得還是蠻有動力的。謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM