在文章认为ASP.NET MVC 3添加评论 [英] ASP.NET MVC 3 Adding comments in article view

查看:130
本文介绍了在文章认为ASP.NET MVC 3添加评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文章模型公开的ICollection<&评论GT;评论{搞定;组; } 和注释的模式。我创建视图的文章(详情查看),我想表现出一切从模型的文章(没有问题)和评论文章和之后的评论再展的形式添加到文章评论(而不是在其他网页,我希望它在鉴于条)。现在我有这样的:

I have article model with public ICollection<Comment> Comments { get; set; } and comment model. I have created view for article (Details view) and I want to show everything from model article (not problem) and comments to article to and after comments then show form for adding comment to article (not in other page, I want it in the view with article). For now I have this:

@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>

它显示的文章并没有对第所有评论局部视图。现在我不知道如何添加形式添加注释。谢谢

It shows article and there is partial view for all comments to article. And now I am not sure how to add form for adding comments. Thank you

修改:这是我的意见控制器和创建方法(vytvorit =创建捷克:)):

Edit: Here is my comment controller and create methods (vytvorit = create in czech :) ):

 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 });
        }

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

When I change @Html.RenderAction to @Html.Action it works. It is showing textbox for comment and I can add comment but there is problem that it not just add textbox but it add my site again (not just partial view but all view) and I am sure I add Create view for comment as partial.

推荐答案

创建一个新的局部视图,使其成为一个强类型的类型注释之一。

Create a new Partial view, make it a strongly typed one of type Comment.

从脚手架模板,选择创建模板。

from the scaffolding templates, choose "Create" template.

处理正常添加注释的新方​​案。

handle the normal add new scenario of the comment.

这部分视图添加到文章详细信息页面。

add this Partial view to the Article details page.

请注意,当您要保存新的评论,您将需要获得托管文章编号。

Note that when you are about to save a new comment, you will need to get the hosting Article ID.

希望这是现在很清楚,如果没有,让我知道。

Hope it's now clear, if not, let me know.

更新:假定你将在AddComment局部视图添加到您的文章详细信息的看法,你可以做,以添加注释以下

update: assuming that you will add the "AddComment" partial view to your "Article details" view, you can do the following in order to add the comment.

1修改 GET (创建)内操作的 CommentController 如下:

1- Modify the GET (Create) action inside your CommentController as the following:

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);            
    }

1 - 使这样的 POST (创建)动作:

[HttpPost]
public ActionResult Create(Comment commentEntity)
{
    // Complete the rest..
}

2 - 您的评论将是这个样子的局部视图:

2- The Partial view for the comment will look something like this:

@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>

        }

3- ArticleDetails页面中,在您需要的附加注释部分显示,使用的RenderAction 方法来呈现类似下面的AddComment局部视图所需要的地方:

3- inside the ArticleDetails page, in the desired place that you need the add comment section to show, use RenderAction method to render the AddComment Partial view like the following:

Html.RenderAction("Create", "Comment",new {articleID = Model.ID});

在previous线将调用 GET (创建)在 CommentColtroller 的行动,并通过当前文章ID,所以AddComment局部视图将与目前的文章编号已经来了填充(这就是我们想要的)。

the previous line will call the GET(Create) action inside CommentColtroller and pass the current Article ID, so the AddComment Partial view will come already populated with the current Article ID (and this is what we want).

就是这样,随时问我,如果它目前尚不清楚,也让我知道,如果它为你工作。

that's it, feel free to ask me if it's not clear yet, and do let me know if it worked for you

这篇关于在文章认为ASP.NET MVC 3添加评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆