Народ! нужна помощь, нужно сделать две фичи! Like и Комент через ajax на Asp.Net Core.
Есть класс Post:
public class Post
{
public int Id { get; set; }
[Display(Name = "Изображение")]
public string ImageUrl { get; set; }
[Display(Name = "Описание")]
public string Description { get; set; }
[Display(Name = "Нравиться!")]
public int Like { get; set; }
[Display(Name = "Количество комментариев")]
public int ComentCount { get; set; }
}
Есть класс Comment
public class Comment
{
[Key] public string CommentId { get; set; }
public int UserId { get; set; }
public ApplicationUser User { get; set; }
public string PostId { get; set; }
public Publication Post { get; set; }
public string Content { get; set; }
public DateTime CommentDate { get; set; }
}
Вот кнопка во вьюшке
<input id="LikeId" type="submit" value="Like" class="btn btn-default" />
которая по задумки должна делать like
так же есть форма для комента
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Post)
</td>
<td>
@Html.DisplayFor(modelItem => item.CommentDate)
</td>
<tr>
<td>
<textarea></textarea>
</td>
<td>
<input type="hidden" name="PostId" value="@item.PostId" />
<input type="hidden" name="UserId" value="0" />
<input type="submit" value="Send" />
</td>
</tr>
<td>
не могу реализовать логику для Like и Коментов.
Для комментариев пробовал делать так:
[HttpPost]
public async Task<IActionResult> Comment(string publicationId, int applicationUserId, string content)
{
Comment comment = new Comment();
if (ModelState.IsValid)
{
comment.UserId = applicationUserId;
comment.PostId = publicationId;
comment.CommentDate = DateTime.Now;
comment.Content = content;
_context.Add(comment);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(comment);
}
Но не работает.
Ajax Пробовал так для комента:
<input id="LikeId" type="submit" value="Like" class="btn btn-default" />
<a asp-area="" asp-controller="Comments" asp-action="Index" title="Comment">Оставить комментарий</a>
<hr />
<div>
Комментарии (@Model.ComentCount)<br/>
<a onclick="openCommentForm(@Model.Id)" style="cursor: pointer">Оставить комментарий</a>
</div>
<div id="[email protected]" style="display:none">
<textarea id="[email protected]"></textarea>
<input type="button" id="postComment" onclick="sendComment('@Model.Id', '@Model.Description')" value="Отправить"/>
</div>
@section Scripts{
<script>
function openCommentForm(publicationId) {
$('#postdiv-' + publicationId).show();
}
function sendComment(publicationId, applicationUserId) {
$.ajax({
url: '@Url.Action("Comment", "Comments")',
type: 'POST',
data: { 'publicationId': publicationId, 'ApplicationUserId': applicationUserId, 'content': $('#posttext-' + publicationId).val() },
success: console.log('все ок'),
error: console.log('Ошибка во время отправки комментария')
});
}
</script>
}
Пример с которого делал:
<div>
Комментарии (@post.CommentsCount)<br/>
<a onclick="openCommentForm(@post.Id)" style="cursor: pointer">Оставить комментарий</a>
</div>
<div id="[email protected]" style="display:none">
<textarea id="[email protected]"></textarea>
<input type="button" id="postComment" onclick="sendComment('@post.Id', '@Model.User.Id')" value="Отправить"/>
</div>
function openCommentForm(postId) {
$('#postdiv-' + postId).show();
}
function sendComment(postId, userId) {
$.ajax({
url: '@Url.Action("Comment", "Post")',
type: 'POST',
data: { 'postId': postId, 'userId': userId, 'content': $('#posttext-' + postId).val() },
success: console.log('все ок'),
error: console.log('Ошибка во время отправки комментария')
});
}
модель:
public class Comment
{
[Key]
public int Id { get; set; }
public string UserId { get; set; }
public User User { get; set; }
public string PostId { get; set; }
public User Post { get; set; }
public DateTime CommentDate { get; set; }
}