0

投票/投票アプリケーションを作成しようとしていますが、オプションに対して行われた投票数を追跡したいと思います。投票が行われると、IDが渡され、IDに応じて、カウンターが1つ増えます。しかし、私が投票すると、もう一方のカウンターが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();
        }
    }

ご覧のように、私は投票されている「ファイト」を通過し、そこからカウンターを変更しています...

4

2 に答える 2

1

次のようなものを使用してみてください。

    if (theFight.VotedID == f1.FighterID)
    {
        theFight.BlueCornerPercent++;
        db.Entry(theFight).Property(p => p.BlueCornerPercent).IsModified = true;
        db.Entry(theFight).Property(p => p.RedCornerPercent).IsModified = false;
        db.SaveChanges();
    }
于 2013-02-15T13:26:28.137 に答える
0

解決策を見つけました。カウンターがリセットされないようにするには、ビューにカウンターの非表示の属性を含める必要がありました。

于 2013-02-19T23:10:30.200 に答える