シンプルなブログサイトで控えめなajaxを紹介しようとしています。ビューポストページがあり、コメントでajaxを有効にします。ajaxを使用してコメントを投稿できますが、そのコメントがすぐに表示されないため、コメントを表示するにはページを更新する必要があります。また、次と前の2つのアクションがあり、投稿間を移動しますが、次または前のボタンを押しても関連するコメントが表示されません。
ここにビューがあります:
@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);
}
コードの何が問題になっていますか?ありがとう。