1

我正在尝试从我的视图中发出 ajax 请求,该请求将同时发布和获取一些数据。我想使用检索到的数据来更新该视图页面上的 div 内容。这是代码:

我的观点:

<div id="NewComment" style="height: auto; width: 850px; background-color: Silver;
    margin-top: 10px;">
    @{
        AjaxOptions ajaxopts = new AjaxOptions { UpdateTargetId = "Comments" };
    }
    @using (Ajax.BeginForm("NewComment", ajaxopts))
    {
        @Html.ValidationSummary()
        <span style="margin-left: 10px">
            @Html.TextAreaFor(x => x.Comments.Comments, new { @class = "Comment" })
        </span>
        @Html.TextBoxFor(x => x.Posts.PostID, new { @class = "PID" })

        <br />
        <span style="margin-left: 10px">
            <input type="submit" value="Post" style="height: 50px; width: 100px; text-align: center;
                font-size: larger" /></span>

    }
</div>
<div id="Comments">
    @{
        if (Model.CommentList != null)
        {
            foreach (var y in Model.CommentList)
            {
        <div id="DisplayComment" style="height: auto; width: 500px; background-color: #559988;
            margin-top: 10px; margin-left: 10px;">
            @y.UserName says:
            <br />
            @y.Comment1
            <br />
        </div>
            }
        }
        else
        {
        <div id="DisplayNoComment" style="height: auto; width: 500px; background-color: Yellow;
            margin-top: 10px; margin-left: 10px;">
            @Html.Label("Be First to make a comment")
        </div>
        }
    }
</div>

我的控制器:

[HttpPost]
    public ActionResult NewComment(PostComment model)
    {
        var business = new Business();
        var entity = new Comment();

        //**For inserting comments
        entity.Comment1 = model.Comments.Comments;
        entity.PostID = model.Posts.PostID; 
        entity.UserID = business.GetUserID(User.Identity.Name);
        entity.UserName = User.Identity.Name;

        business.PostComment(entity);
        //For inserting comments**


        //**for getting comments
        var viewmodel = new PostComment();
        if (business.GetAllComment(model.Posts.PostID) == null)
        {
        }
        else
        {
            viewmodel.CommentList = business.GetAllComment(model.Posts.PostID);
        }
        //for getting comments**

        return View("Comments", viewmodel);
    }

我正在返回一个名为注释的部分视图,其中包含所需的数据,一切正常,但存在有线问题。虽然我在部分视图中没有使用任何母版页,但它仍然显示内容表单母版页。因此,原始视图页面显示母版页中的内容,如页眉和页脚两次。请帮忙。

4

1 回答 1

0

在您看来,请在顶部包含以下代码行

@{
   Layout = null;
}
于 2012-10-30T05:40:36.773 回答