0
我正在尝试创建一个投票/投票应用程序,并希望跟踪为选项所做的投票数量。当进行投票时,会传入一个id并取决于id - 计数器会增加1。但是当我投票时,它将另一个计数器重新设置为一个。在模型中保留一个计数器
// 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();
}
}
正如所看到的,我在路过正被付诸表决,并改变从那里计数器的“战斗” ......
你是否在实例化你的类的新实例? – 2013-02-14 20:29:16
我想我可能是,嗯。 – 2013-02-14 20:30:24
您发布的代码片段不足。请发布您的整个控制器。 – 2013-02-14 20:30:43