ASP.NET MVC 3在文章视图中添加注释

Lib*_*tal 4 asp.net-mvc asp.net-mvc-3

我有文章模型 public ICollection<Comment> Comments { get; set; }和评论模型.我已经为文章(详细信息视图)创建了视图,我希望显示从模型文章(不是问题)和评论到文章到评论之后的所有内容然后显示用于向文章添加评论的表单(不在其他页面中,我希望它在用文章查看).现在我有这个:

@model SkMoravanSvitavka.Models.Article

@{
    ViewBag.Title = "Zobrazit";
}

<h2>Zobrazit</h2>

<fieldset>
    <legend>Article</legend>

    <div class="display-label">Title</div>
    <div class="display-field">@Model.Title</div>

    <div class="display-label">Text</div>
    <div class="display-field">@Model.Text</div>

    <div class="display-label">PublishedDate</div>
    <div class="display-field">@String.Format("{0:g}", Model.PublishedDate)</div>
</fieldset>

@if (Model.Comments != null)
{
    foreach (var comment in Model.Comments)
    {
        @Html.Partial("_Comment", comment)
    }
}



<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.ArticleID }) |
    @Html.ActionLink("Back to List", "Index")
</p>
Run Code Online (Sandbox Code Playgroud)

它显示了文章,并且对文章的所有评论都有部分视图.现在我不知道如何添加表单来添加评论.谢谢

编辑:这是我的评论控制器和创建方法(vytvorit =在捷克创建:)):

 public ActionResult Vytvorit(int articleID)
        {
            var newComment = new Comment();
            newComment.articleID = articleID; // this will be sent from the ArticleDetails View, hold on :).
           newComment.Date = DateTime.Now;
            return View(newComment);  
        } 

        [HttpPost]
        public ActionResult Vytvorit(Comment commentEntity)
        {
                db.Comments.Add(commentEntity);
                db.SaveChanges();
                return RedirectToAction("Zobrazit", "Clanek", new { id = commentEntity.articleID });
        }
Run Code Online (Sandbox Code Playgroud)

当我改变@Html.RenderAction@Html.Action它的工作原理.它显示了注释的文本框,我可以添加评论,但存在的问题是它不只是添加文本框,但它再次添加我的网站(不只是局部视图,但所有视图),我相信我添加创建视图对此事发表评论的部分.

Moh*_*lam 7

创建一个新的部分视图,使其成为类型为Comment的强类型视图.

从脚手架模板中,选择"创建"模板.

处理正常添加评论的新方案.

将此部分视图添加到文章详细信息页面.

请注意,当您要保存新评论时,您需要获取托管文章ID.

希望现在很清楚,如果没有,请告诉我.

更新:假设您将"AddComment"部分视图添加到"文章详细信息"视图中,您可以执行以下操作以添加注释.

1-修改GET您内部的(创建)操作CommentController,如下所示:

public ActionResult Create(int articleID)
    {
            var newComment = new CommentEntity();
            newComment.articleID = articleID; // this will be sent from the ArticleDetails View, hold on :).

            return View(newComment);            
    }
Run Code Online (Sandbox Code Playgroud)

1- POST像这样制作(创建)动作:

[HttpPost]
public ActionResult Create(Comment commentEntity)
{
    // Complete the rest..
}
Run Code Online (Sandbox Code Playgroud)

2-评论的部分视图如下所示:

@model NGO.Models.Comment

@using (Html.BeginForm())
        {            
            @Html.ValidationSummary(true)
            <div class="addcommentbox">
                <h2> Add Comment </h2>
                @Html.TextAreaFor(model => model.Description)
                <div class="ErrorMessage">
                @Html.ValidationMessageFor(model => model.Description)
                </div>                    
                <input id="addComment" type="button" onclick="" value="Add" />
            </div>

        }
Run Code Online (Sandbox Code Playgroud)

3-在ArticleDetails页面内,在您需要添加注释部分显示的所需位置,使用RenderAction方法呈现AddComment部分视图,如下所示:

Html.RenderAction("Create", "Comment",new {articleID = Model.ID});
Run Code Online (Sandbox Code Playgroud)

上一行将调用GET内部的(Create)操作CommentColtroller并传递当前的文章ID,因此AddComment Partial视图将填充当前的文章ID(这就是我们想要的).

就是这样,请随时问我是否还不清楚,如果它对您有用,请告诉我