How to load a PartialView to create a comment

1

I'm working on a project to learn ASP.NET MVC and Razor. I currently have a post template with a view , and a view associated with the model class comment to create a new comment. I would like to know how to do to put the view comment creation at the end of the post , and when to submit the comment to the post as an associate (I have a% of the comment to make the association). I inserted manually some comments in the database and they appear in the right place because I put PostId manually, which is not functional. See 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>
    
asked by anonymous 14.12.2016 / 20:53

1 answer

1

The beginning is ok, but it will be necessary to change some things so that it makes sense to use 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 are hidden , and we will have to fill them when calling Partial :

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

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

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

return RedirectToAction("Details", "Posts", new { PostID = Model.PostID });
    
14.12.2016 / 21:08