I like only to update the @Html.Partial("_CommentSection") after Button type="submit" id="PostButton" is clicked by the user.
At the moment I reload the whole page to see the new comment. But I think its not nice to have it.
Is there a way to do it maybe and you can help me a bit more to come to my goal.
Maybe with jquery - but I didn`t use this before and don't know how to do
I have in asp.net project a partial view of this code:
@model MyProject.Models.Home
@foreach (var comment in MyProject.Controllers.HomeController.GetAllComments(Model.Id))
{
<br />
<div>
<div>
<a>
@MyProject.Controllers.HomeController.GetUserName(comment.UserId)
</a>
added a comment -
<span>
<time>@comment.CommentCreated.ToString()</time>
</span>
<span id="edit-delete-comment">
@using (Html.BeginForm("DeleteComment", "Home"))
{
@Html.AntiForgeryToken()
if (MyProject.Controllers.HomeController.CheckIfUserIsCreator(User.Identity.Name, comment.CommentId))
{
if (!MyProject.Controllers.HomeController.CheckDateOfComment(comment.CommentId))
{
<div class="form-actions no-color">
@Html.Hidden("returnUrl", this.Request.RawUrl)
@Html.Hidden("CommentId", comment.CommentId)
<input type="submit" value="Delete" class="t" />
</div>
}
}
}
</span>
</div>
<br />
<div>
<p>
@comment.CommentText
</p>
</div>
</div>
<hr />
}
@using (Html.BeginForm("CreateComment", "Home", FormMethod.Post, null))
{
@Html.AntiForgeryToken()
<div class="mt-5 d-flex flex-row">
@Html.Hidden("returnUrl", this.Request.RawUrl)
@Html.Hidden("HomeId", Model.Id)
<textarea class="form-control" name="CommentText"></textarea>
<button class="btn btn-secondary btn-block mt-2 post-btn" type="submit" id="PostButton">Post</button>
</div>
}
My View:
<div id="partial-update">
@Html.Partial("_CommentSection")
</div>
My Controller (update page after post is commented out)
[HttpPost]
[AllowOnlyOneRequest]
[ValidateAntiForgeryToken]
public void CreateComment([Bind(Include = "CommentId,CommentText,CommentCreated,UserId,HomeId")] Comment comment, string returnUrl, int HomeId)
{
if (ModelState.IsValid)
{
comment.CommentCreated = DateTime.Now;
comment.UserId = db.Users.FirstOrDefault(i => i.UserEmail == User.Identity.Name).UserId;
comment.HomeId = HomeId;
db.Comments.Add(comment);
db.SaveChanges();
//return Redirect(returnUrl);
}
//return Redirect(returnUrl);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…