将列表中的数据保存到另一个表

时间:2015-03-23 23:32:24

标签: c#

[HttpPost]
    public ActionResult AddVotes(List<CampaignManager_tbl> list, int events_category_id, int candidates_info_id) {

        if (ModelState.IsValid)
        {
            var events = db.Events_Info_tbl.Where(x => x.is_active == true).FirstOrDefault();
            var username = User.Identity.Name;
            var getID = db.Account_Info_tbl.Where(x => x.username == username).FirstOrDefault();

            foreach (var i in list)
            {
                Votes_tbl vote = new Votes_tbl();

                vote.candidates_info_id = i.candidates_info_id;
                vote.C_voters_info_id = getID.account_info_id;
                vote.events_info_id = events.events_info_id;
                vote.events_category_id = i.events_category_id;
                vote.votes_history = true;
                db.Votes_tbl.Add(vote);
            }
            db.SaveChanges();
            RedirectToAction("Index");
        }

        return View(list);
    }

这是我的代码,我要将列表中的数据保存到数据库中,但它无法正常工作。 List<CampaignManager_tbl> List包含要保存在Votes_tbl中的数据。

这段代码有什么问题?我将如何保存这些数据?

2 个答案:

答案 0 :(得分:1)

将数据保存在循环之外。

[HttpPost]
public ActionResult AddVotes(List<CampaignManager_tbl> list, int category_id, int candidates_info_id) {

if (ModelState.IsValid)
    {
        var events = db.Events_Info_tbl.Where(x => x.is_active == true).FirstOrDefault();
        var username = User.Identity.Name;
        var getID = db.Account_Info_tbl.Where(x => x.username == username).FirstOrDefault();

        foreach (var i in list)
        {
            Votes_tbl vote = new Votes_tbl();

            vote.candidates_info_id = i.candidates_info_id;
            vote.C_voters_info_id = getID.account_info_id;
            vote.events_info_id = events.events_info_id;
            vote.events_category_id = category_id;
            vote.votes_history = true;
            db.Voters_Info_tbl.Add(vote);


        }
        db.SaveChanges();
        RedirectToAction("Index");
    }

    return View(list);
}

答案 1 :(得分:0)

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AddVotes(List<CampaignManager_tbl> list)
    {
        if (ModelState.IsValid)
        {
            var events = db.Events_Info_tbl.Where(x => x.is_active == true).FirstOrDefault();
            var username = User.Identity.Name;
            var getID = db.Account_Info_tbl.Where(x => x.username == username).FirstOrDefault();

            foreach (var i in list)
            {
                int val = 1;
                bool y = Convert.ToBoolean(val);
                if (i.isSelected == y) {
                    Votes_tbl vote = new Votes_tbl();

                    vote.candidates_info_id = i.candidates_info_id;
                    vote.C_voters_info_id = getID.account_info_id;
                    vote.events_info_id = events.events_info_id;
                    vote.events_category_id = i.events_category_id;
                    vote.votes_history = true;
                    db.Votes_tbl.Add(vote);
                }              
            }
            db.SaveChanges();
            return RedirectToAction("Index");

        }
        return View(list);
    }

我已经达成了这个解决方案,但无论如何,谢谢你提供建议。它有很大帮助。谢谢。