I am trying to create a poll/voting application and want to keep track of the amount of votes that are made for the options. When a vote is made, an id is passed in and depending on the id – the counter is increased by one. But when I vote it sets the other counter back to one.
// ID of the option selected:
public int VotedID { get; set; }
Counters for the options:
public int BlueCornerPercent { get; set; }
public int RedCornerPercent { get; set; }
// Snippet of code - here is where I increase the counters. theFight is an instance of the model/entity.
public void HandleVotes(Fight fight)
{
// Get full fight details:
Fight theFight = db.Fights.Find(fight.FightId);
// Get fighters id's in fight:
var f1 = (from l in theFight.Fighters
select l).First();
var f2 = (from l in theFight.Fighters
select l).Last();
if (theFight.VotedID == f1.FighterID)
{
theFight.BlueCornerPercent++;
db.SaveChanges();
}
else if (theFight.VotedID == f2.FighterID)
{
theFight.RedCornerPercent++;
db.SaveChanges();
}
}
As seen I’m passing in the “Fight” that is being voted on and changing the counter from there…
I found the solution, I needed to include hidden attributes for the counters in my view to keep them from resetting.