Give IT
Give IT

Reputation: 200

Razor Ajax Helper does not replacing UpdateTargetId

I have this Ajax Begin Form

using (Ajax.BeginForm("ShowAllComments", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "CommentsList" }))
        {
            <div class="form-horizontal">
                <div class="form-group" style="visibility:hidden;height:3px;width:3px;">
                    <div class="col-md-10">
                        <input class="form-control text-box single-line" required id="article_id" name="article_id" type="text" value="@Model.news.id" />
                        <span class="field-validation-valid text-danger" data-valmsg-for="article_id" data-valmsg-replace="true"></span>
                    </div>

                </div>
                <input class="show_all_comments" type="submit" value="Show All Comments.." />
            </div>
        }  

Which sends the post to this method

[HttpPost]
    public ActionResult ShowAllComments(int article_id)
            {
                    var innerJoinQuery =
                    (from comment in db.Set<Comment>()
                     join person in db.Set<Person>() on comment.create_user equals person.id
                     where comment.accepted && comment.category == 2 && comment.place_id == article_id
                     select new CommentView { userfullname = person.firstname + " " + person.lastname, description = comment.description, create_date = comment.create_date }).OrderBy(x => x.create_date).ToList();
                    masterlayout.comment_view_list = innerJoinQuery;
                    return PartialView("_CommentsList", masterlayout);

            }

And this (_CommentsList) Partial View

@model Spearfishing.Data.MasterLayoutView

<div id="CommentsList">
    @foreach (var item in Model.comment_view_list)
    {
        <div class="comment">
            <div class="comment_user">@item.userfullname</div>
            <div class="comment_descr">@Html.Raw(item.description)</div>
            <div class="comment_date"><i class="fa fa-calendar" aria-hidden="true"></i> @item.create_date.ToString("dd/MM/yy - HH:mm")</div>
        </div>
    }
</div>

The problem is that my Ajax.BeginFrom does not replace the updatetargetid -> CommentsList.. Instead of this it adds a new div in the CommentsList div .. with id CommentsList something like this <div id="CommentsList"><div id="CommentsList">

can someone help me please.. Where is my fault?

Upvotes: 0

Views: 275

Answers (2)

user4527919
user4527919

Reputation:

Try changing InsertionMode.Replace with InsertionMode.ReplaceWith :)

Upvotes: 1

mejiajuanbta
mejiajuanbta

Reputation: 176

In Ajax.BeginForm add htmlAttributes: id = "CommentsList"

In _CommentsList delete (<div id="CommentsList"><div id="CommentsList">)

@model Spearfishing.Data.MasterLayoutView

    @foreach (var item in Model.comment_view_list)
    {
        <div class="comment">
            <div class="comment_user">@item.userfullname</div>
            <div class="comment_descr">@Html.Raw(item.description)</div>
            <div class="comment_date"><i class="fa fa-calendar" aria-hidden="true"></i> @item.create_date.ToString("dd/MM/yy - HH:mm")</div>
        </div>
    }

This will replace the html in the form tag

Upvotes: 1

Related Questions