So I have 2 controller classes;
AnnouncementsController, this just generates a homepage of sorts with posts from users on it.
// GET: Announcements
public async Task<IActionResult> Index()
{
var announcements = await _context.Announcement.ToListAsync();
announcements = announcements.OrderByDescending(x => x.CreatedOn).ToList();
foreach (var ann in announcements)
{
ann.TimeAgo = ann.CreatedOn.TimeAgo();
}
var users = await _context.Users.ToListAsync();
var comments = await _context.Comment.ToListAsync();
AnnouncementAndCommentsViewModel aacVM = new AnnouncementAndCommentsViewModel();
AnnouncemenstViewModel announcemenstViewModel = new AnnouncemenstViewModel();
announcemenstViewModel.Announcement = announcements;
announcemenstViewModel.User = users;
announcemenstViewModel.Comment = comments;
CommentViewModel commentViewModel = new CommentViewModel();
aacVM.announcemenstViewModel = announcemenstViewModel;
aacVM.commentViewModel = commentViewModel;
ViewData.Add("currentUserID",GetCurrentUser().Id);
return View(aacVM);
}
Then I have the CommentsController
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(CommentViewModel commentViewModel)
{
if (ModelState.IsValid)
{
var comment = new Comment();
comment.CreatedOn = DateTime.Now;
comment.Body = commentViewModel.Comment.Body;
Announcement announcement = GetAnnouncement(commentViewModel.Announcement.AnnouncementID);
comment.Announcement = announcement;
ApplicationUser user = GetCurrentUser();
comment.User = user;
_context.Add(comment);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(commentViewModel);
}
From my AnnouncementController Index view, I have a list of posts and then I want to be able to write and add comments directly in that view as opposed to going to another view.
I have 2 ViewModels, one for storing announcements and one for comments.
I'm trying to call the Create Post method in the Comments Controller with its argument that accepts a CommentViewModel although I can't figure it out. I keep getting sent to the Create Get Method with the body filled in.
Here's my index view and how I'm trying to post the data to the CommentsController
@model Treharris.Models.AnnouncementAndCommentsViewModel;
.
.
.
@foreach (var item in Model.announcemenstViewModel.Announcement)
{
.
.
.
<div class="announcement-body">
<p>@Html.DisplayFor(modelItem => item.Body)</p>
</div>
<div class="comments">
<p id="comment">Comments</p>
@using(Html.BeginForm("Create","Comments", FormMethod.Post,
new { cvm = Model.commentViewModel }))
{
@@Model.commentViewModel.Announcement = item;
@Html.HiddenFor(m => m.commentViewModel.Announcement)
@Html.TextBoxFor(m => m.commentViewModel.Comment.Body,
new { placeholder = "Comment body" })
<input type="submit" value="Create" />
}