I'm trying to pull a list of Ids and Usernames from an Audit table that is populated with Ids from a separate table, and usernames of people who voted on an Idea.
The flow should be:
I'm trying to use foreach to loop through the IdeaBoardVote table to find matches, but I get the following error:
'System.Web.Mvc.SelectListItem' does not contain a definition for 'IdeaId'
Here is my Controller code to populate the list:
[HttpGet]
public ActionResult Index(int page = 1, string message = "")
{
ViewBag.Message = message;
if (!Request.IsAuthenticated)
{
ViewBag.Message = "You must be logged in to vote on or create new ideas.";
}
//Populate list of IdeaIds and Usernames from IdeaBoardVote table
var ideaBoardVotes = db.IdeaBoardVotes.ToList();
ViewBag.IdeaVoters = new SelectList(ideaBoardVotes, "IdeaId", "Username");
var ideaBoards = db.IdeaBoards.OrderByDescending(i => i.VoteCount).Take(100);
int pageSize = 10;
return View(ideaBoards.ToPagedList(page, pageSize));
}
Here is what I have in my View:
@{bool hasVoted = false;}
foreach (var vote in ViewBag.IdeaVoters)
{
if (vote.IdeaId == idea.Id && vote.Username == User.Identity.Name)
{
hasVoted = true;
}
}
if (!hasVoted)
{
<a href="@Url.Action("IncreaseVote", "IdeaBoard", new { id = idea.Id })" class="btn btn-default btn-sm width-100 margin-bottom text-left">
Vote <span class="glyphicon glyphicon-thumbs-up blue"></span>
</a>
}
What I'm missing that I'm getting the error message?
Thank you @Sreenath. Your advise is exactly what I was missing. Here is how I solved this simple issue. In my foreach loop, I was using vote.IdeaId and vote.Username. I changed those to vote.Value and Vote.Text.
if (vote.Value == idea.Id.ToString() && vote.Text == User.Identity.Name)
{
hasVoted = true;
}
Also, changed where the bool was declared so it would be local scope instead of page scope.
Those little changes made the difference. So again, thank you @Sreenath.