0

我是 MVC 的新手,正在尝试以下场景,但对如何继续感到震惊。

在我的网页中,我有几个部分,每个部分下面都有评论。为了获取评论,我在控制器中编写了一个函数,如下所示

public ActionResult ShowPreviousComments()
        {
            Comments com = new Comments();

            LstComments savedComments = new LstComments();

            ///Entity code here


            foreach (var item in comments)
            {
                com.comments = item.Comments;
                com.dTime = item.Time;

                savedComments.lstCommet.Add(com);

            }

            return View();
        }

和下面的模型数据,以便我可以在视图中获取列表

public class Comments
    {
        public string comments { get; set; }

        public DateTime? dTime { get; set; }

        public int airPortId { get; set; }

    }

    public class  LstComments
    {
        public List<Comments> lstCommet { get; set; }
    }

我的疑问是如何在每个部分的页面加载期间点击控制器?

对不起,如果这听起来很傻或有什么错误。请张贴如果我能以更好的方式做到这一点

谢谢

4

2 回答 2

4

控制器

public ActionResult ShowPreviousComments()
{
    Comments com = new Comments();
    LstComments savedComments = new LstComments();

    ///Entity code here


    foreach (var item in comments)
    {
        com.comments = item.Comments;
        com.dTime = item.Time;

        savedComments.lstCommet.Add(com);
    }

    return PartialView(savedComments);
}

看法

@Html.Action("ShowPreviousComments", "SomeController") 

依我看来

控制器

public ActionResult ShowPreviousComments()
{
    // Entity code here
    // var comments = entity.comments;

    return PartialView(comments);
}

Index.cshtml(包含自己的内容和注释的主视图)

// some main view content

// main view comments...
@Html.Action("ShowPreviousComments", "SomeController")

ShowPreviousComments.chtml(包含先前评论的部分视图)

@model IEnumerable<comments>

<div class="comments_container>
    foreach(var item in Model)
    {
        <div class="comment_body">@item.Comments</div>
        <div class="comment_time">@item.Time</div>
    }
</div>
于 2013-02-25T14:38:57.297 回答
0

也许你可以尝试一个脚本来调用视图部分的加载控制器

于 2014-02-15T15:29:13.453 回答