ASP.NET MVC入門教程(三)文章評論內容的加載與顯示

ASP.NET MVC入門教程(三)html

本部分主要實現內容頁中評論內容的加載與顯示瀏覽器

1、建立Comment控制器code

1.在Controllers文件夾下,建立Comment控制器,控制器名稱爲CommentControllerhtm

2、建立GetCommentByArticleId方法教程

1.引入命名空間it

using MyStudy.Models;

2.建立數據上下文io

private MyEFContainer db = new MyEFContainer();

3.建立GetCommentByArticleId方法,代碼以下入門

public PartialViewResult GetCommentByArticleId(int id)
        {
            List<tb_comment> comments = db.tb_comment
                .Where(m => m.ArticleId == id)
                .ToList();
            return PartialView(comments);
        }

3、建立分部視圖table

1.建立分部視圖class

在方法內部右鍵單擊,選擇「添加視圖」

2.修改視圖以下

@model IEnumerable<MyStudy.Models.tb_comment>

<div class="row">
    <h4>評論內容</h4>
    <table class="table">
        @foreach(var item in Model)
        {
            <tr>
                <td>@item.Content</td>
                <td>@item.CreateDate</td>
            </tr>
        }
    </table>
</div>

3.在文章內容頁加載部分視圖

在Details視圖中加入如下代碼,實現評論內容的加載

<!--加載評論內容-->
@Html.Action("GetCommentByArticleId", "Comment", new { id=Model.Id})

4、運行並查看瀏覽器顯示結果

相關文章
相關標籤/搜索