Razor Ajax Helper不替换UpdateTargetId

时间:2016-08-06 17:00:15

标签: c# jquery ajax asp.net-mvc razor

我有这个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>
        }  

将帖子发送到此方法

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

            }

这个(_CommentsList)部分视图

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

问题是我的Ajax.BeginFrom没有取代updatetargetid - &gt; CommentList ..而不是这个,它在CommentsList div中添加了一个新的div .. id为这个<div id="CommentsList"><div id="CommentsList">

的注释列表 有人可以帮帮我..我的错在哪里?

2 个答案:

答案 0 :(得分:1)

在Ajax.BeginForm中添加htmlAttributes:id =&#34; CommentsList&#34;

在_CommentsList中删除(<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>
    }

这将替换表单标签中的html

答案 1 :(得分:1)

尝试使用InsertionMode.ReplaceWith更改InsertionMode.Replace:)