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:
- See an Idea
- Click the Vote button
- Add the Idea Id, logged in User, and DateTime they clicked the Vote button to the IdeaBoardVote table.
- Disable Vote button if after querying the IdeaBoardVote table, the IdeaId and Username match an IdeaBoard records Id and Username.
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?
question from:
https://stackoverflow.com/questions/65835498/pull-list-of-records-from-audit-table 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…