2012-10-18 14 views
0

我想在我的簡單博客網站中介紹一些不顯眼的ajax,這裏有一個viewpost頁面,我希望評論能夠成爲ajax。我可以使用ajax發佈評論,但該評論沒有立即顯示,頁面需要刷新才能看到評論。此外,我還有兩項操作,其次是前一個&,它將瀏覽帖子,但按下一個或上一個按鈕時不會顯示相關注釋。如何在mvc中使用不顯眼的ajax 3即時獲得效果

這裏是視圖:

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

什麼是錯誤的代碼?謝謝。

回答

相關問題