How to load a Partialview to create a comment

Asked

Viewed 67 times

1

I am working on a project to learn ASP.NET MVC and Razor. Right now I have a model of post with a view, and a view associated with model class comment to create a new comment. I wanted to know how to put the view creation of commentary at the end of the post, and when to submit as associate the comment to the post (I have a variable PostId in the comment template to make the association). I manually entered some comments in the database and they appear in the right place because I put the PostId manually, which is not functional. See the view to create a comment:

    @model shanuMVCUserRoles.CommentSet

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>CommentSet</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.MemberID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.MemberID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.MemberID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PostID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PostID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PostID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

1 answer

1


The beginning is ok, but it will be necessary to change some things so that it makes sense to use as Partialview:

@model shanuMVCUserRoles.CommentSet

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Adicione um comentário</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.MemberID)    
        @Html.HiddenFor(model => model.PostID)

        <div class="form-group">
            @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.Content, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Criar" class="btn btn-default" />
            </div>
        </div>
    </div>
}

Note that PostID and MemberID sane hidden, and we’ll have to fill them in by calling Partial:

@Html.Partial("_AdicionarComentario", new CommentSet { PostID = Model.PostID, MemberID = User.Identity.GetUserId() })

This can be loaded on View Details, or in the View that displays the Post. The name you choose.

When you enter the comment, you redirect the page to the Post, and not for the comment.

return RedirectToAction("Details", "Posts", new { PostID = Model.PostID });
  • @Html.Partial("_Additionrcomment", new Commentset { Postid = Model.Postid, Memberid = User.Identity.Getuserid() }) only this line has helped me more than what I found before, very good the answer!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.