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);
}
This is the third time I’ve changed this post today but I worked it out.
I feel compelled to thank you and would like to mark you down as an answer as as I found a lot of great info in your web site but I cant, I guess? I don’t like marking myself as the answer so is there a way I can just mark this as resolved?
My main problem is I really needed to learn a little more MVC this week!
My view is a simple comment form and all it had to know about was the current posts ID, but the modal I was using was a comment modal. I tried Including(“Posts”) in comments and Including(“Comments”) in posts…lol, and then just going back and forward, then
I tried this in my Index action,
public ActionResult Index()
{
Post post = db.Posts.Single(p => p.PostCurrent == true);
return View(post);
}
and updated this,
@model TMPBlog.Models.Comment
to this in my view,
@model TMPBlog.Models.Post
and it works, still a lot to learn, MVC is a lot different to the way I used to do things!
bojangles
Participant
976 Points
644 Posts
Where clause
Mar 05, 2012 02:46 AM|LINK
Hi,
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); }How should I do this?
Cheers,
Mike.
Mikesdotnett...
All-Star
155645 Points
19985 Posts
Moderator
MVP
Re: Where clause
Mar 05, 2012 12:54 PM|LINK
How do you define which post is the current post?
Web Pages CMS | My Site | Twitter
bojangles
Participant
976 Points
644 Posts
Re: Where clause
Mar 05, 2012 09:17 PM|LINK
Hi Mike,
This is the third time I’ve changed this post today but I worked it out.
I feel compelled to thank you and would like to mark you down as an answer as as I found a lot of great info in your web site but I cant, I guess? I don’t like marking myself as the answer so is there a way I can just mark this as resolved?
My main problem is I really needed to learn a little more MVC this week!
My view is a simple comment form and all it had to know about was the current posts ID, but the modal I was using was a comment modal. I tried Including(“Posts”) in comments and Including(“Comments”) in posts…lol, and then just going back and forward, then I tried this in my Index action,
public ActionResult Index() { Post post = db.Posts.Single(p => p.PostCurrent == true); return View(post); }and updated this,
to this in my view,
and it works, still a lot to learn, MVC is a lot different to the way I used to do things!
Cheers,
Mike.