Question

I'm trying to make a ajax request from my view, which will post & get some data at the same time. I want to use the retrieved data to update a div content on that view page. Here is the code:

My View:

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

My Controller:

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

I'm returning a partial view named comments with the required data, everything is working fine but having a wired problem. Though I'm not using any master page in my partial view but still its showing the contents form master page. So the original view page showing the contents from master page like header & footer twice. Please help.

Was it helpful?

Solution

In your view, include the below line of code on the top

@{
   Layout = null;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top