0

我试图在我的简单博客站点中引入一些不显眼的 ajax,有一个 viewpost 页面,我希望评论启用 ajax。我可以使用 ajax 发布评论,但该评论不会立即显示,需要刷新页面才能看到评论。另外,我有两个动作作为下一个和上一个,它们将浏览帖子,但是在按下下一个或上一个按钮时不会显示相关评论。

这是视图:

@model  Presentation.Models.PostComment
@{
ViewBag.Title = "ViewPost";
Layout = "~/Views/Shared/_Layout.cshtml";

}
<html>
<head>
<title>View Post</title>
</head>
<body>
<div id="viewStory" style="height: auto; width: 850px; background-color: Silver">
    <h3>@Html.DisplayFor(x => x.Posts.PostTitle)</h3>
    <br />
    <br />
    @Html.Raw(System.Web.HttpUtility.HtmlDecode(Model.Posts.PostStory))
    <br />
    @{
        var PID = Model.Posts.PostID;
    }
    @Html.TextBoxFor(x => x.Posts.PostID, new { @class = "PIDPost" })
</div>
<div id="Button" style="height: auto; width: 850px">
    <span id="Next" style="float: left; margin-top: 10px; margin-left:  10px;">@Html.ActionLink("Previous", "PreviousPost", "Home", new { @model = PID }, null)</span>
    <span id="Previous" style="float: right; margin-top: 10px; margin-right: 10px;">@Html.ActionLink("Next", "NextPost", "Home", new { @model = PID }, null)</span>
    <br />
</div>
<br />
<div id="CommetPartial" style="height: auto; width: 850px; background-color: Silver;">
    @Html.Label("Comment:")
    @*@Html.Partial("Comment", @ViewData)*@
</div>
<div id="NewComment" style="height: auto; width: 850px; background-color: Silver;
    margin-top: 10px;">
    @{
        AjaxOptions ajaxopts = new AjaxOptions { HttpMethod = "Post" };
    }
    @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>



    }
    @{
        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.Comments
            <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>

以下是与视图关联的操作方法:

[HttpGet]
   public ActionResult ViewPost()
   {
       var business = new Business();

       var PostEntity=business.GetAllPost();

       var ViewModel = new PostComment();

       //**For getting Post
       if (PostEntity != null)
       {
           ViewModel.Posts.PostTitle = PostEntity.First().PostTitle;
           ViewModel.Posts.PostStory = PostEntity.First().PostStory;
           ViewModel.Posts.PostID = PostEntity.First().PostID;
       }

       else
       {
       }



       //**For getting comments
       if (business.GetAllComment(PostEntity.First().PostID) == null)
       {

       }
       else
       {
           ViewModel.CommentList = business.GetAllComment(PostEntity.First().PostID);
       }
       //For getting comments**




       return View("ViewPost", ViewModel);
   }

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

        //**For inserting comments
        entity.Comments = 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("ViewPost", viewmodel);
    }



    [HttpGet]
    public ActionResult NextPost(int model)
    {
        var business = new Business();

        var temp=business.GetNextPost(model);

        var viewmodel = new PostComment();
        if (temp != null)
        {
            viewmodel.Posts.PostID = temp.First().PostID;
            viewmodel.Posts.PostTitle = temp.First().PostTitle;
            viewmodel.Posts.PostStory = temp.First().PostStory;
        }
        else
        {
        }

        if (business.GetAllComment(model) == null)
        {

        }
        else
        {
            viewmodel.CommentList = business.GetAllComment(model);
        }
        return View("ViewPost", viewmodel);

    }

    [HttpGet]
    public ActionResult PreviousPost(int model)
    {
        var business = new Business();
        var temp=business.GetPreviousPost(model);

        var viewmodel = new PostComment();
        if (temp != null)
        {
            viewmodel.Posts.PostID = temp.First().PostID;
            viewmodel.Posts.PostTitle = temp.First().PostTitle;
            viewmodel.Posts.PostStory = temp.First().PostStory;
        }
        else
        {
        }


        if (business.GetAllComment(model) == null)
        {

        }
        else
        {
            viewmodel.CommentList = business.GetAllComment(model);
        }

        return View("ViewPost", viewmodel);
    }

代码有什么问题?谢谢。

4

1 回答 1

4

你应该看看那些 ajaxOptions。我相信您需要指定 updatetargetid 来告诉表单它应该使用返回的数据更新哪个 id。如果要返回 JSON,则可以使用 onSuccess ajaxoption 触发 javascript 函数。

Onsuccess: 如何使用 Ajax.BeginForm OnSuccess 和 OnFailure 方法?

updatetargetid: 在 ASP.NET MVC 3 Razor 中使用 Ajax.BeginForm

于 2012-10-18T08:00:47.763 回答