I have the following code that selects a bunch of comments from a database, but I only ever have one current post and I would like to deal only with comments regarding the current Post.
public ActionResult Index()
{
ViewBag.PostCommentFK = new SelectList(db.Posts, "PostID", "PostTitle");
return View();
}
I guess all I need is to add a where clause to the statement, “PostCurrent” is a Boolean and there can only ever be one “True” PostCurrent.
Currently in my view I have a hidden field with the value of 1 in it, so comments are only added to post 1, but would like that to be the current post.
@Html.HiddenFor(model => model.PostCommentFK, new { Value = 1 })
I then have a [HttpPost] method to SaveChanges,
[HttpPost]
public ActionResult Index(Comment comment)
{
if (ModelState.IsValid)
{
db.Comments.AddObject(comment);
db.SaveChanges();
return RedirectToAction("CommentResponse");
}
ViewBag.PostCommentFK = new SelectList(db.Posts, "PostID", "PostTitle", comment.PostCommentFK);
return View(comment);
}
I think you need to hide
PostIDinsteadvalueThen :