Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

c# - Pull List of Records from Audit Table

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:

  1. See an Idea
  2. Click the Vote button
  3. Add the Idea Id, logged in User, and DateTime they clicked the Vote button to the IdeaBoardVote table.
  4. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably the column name IdeaId There is something else in the IdeaBoardVotes table

ViewBag.IdeaVoters = new SelectList(ideaBoardVotes, "IdeaId", "Username");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...