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 に答える