我有情况,我需要从里面的单选按钮列表中获取选定的值 中继器控制。我的问题是我在转发器中有多个行但是当我将所选值添加到数据库时,它只从一行插入数据。这是我的代码。
public double evalute()
{
qresponsedetail responses = new qresponsedetail();
qrespons qresponse = new qrespons();
qresponse.quizid = quizid;
qresponse.suerid = username;
qresponse.score = 0;
context.AddToqresponses(qresponse);
context.SaveChanges();
foreach (RepeaterItem item in Repeater1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
string ansid = (item.FindControl("RadioButtonList1") as RadioButtonList).SelectedItem.Value.ToString();
string qid = (item.FindControl("qid") as HiddenField).Value.ToString();
responses.responseid = qresponse.responseid;
responses.answerid = Convert.ToDecimal(ansid);
responses.questionid = Convert.ToDecimal(qid);
context.AddToqresponsedetails(responses);
context.SaveChanges();
}
}
return 0;
}
我的第二个要求是我如何将所有选定的值添加到列表中然后使用EF插入。我在函数中使用了两次context.savechanges,我怎么能只用一个context.savechanges来实现。感谢
答案 0 :(得分:0)
public double evalute()
{
qrespons qresponse = new qrespons();
qresponse.quizid = quizid;
qresponse.suerid = username;
qresponse.score = 0;
context.AddToqresponses(qresponse);
context.SaveChanges();
foreach (RepeaterItem item in Repeater1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType ==
ListItemType.AlternatingItem)
{
qresponsedetail responses = new qresponsedetail();
string ansid = (item.FindControl("RadioButtonList1") as RadioButtonList).SelectedItem.Value.ToString();
string qid = (item.FindControl("qid") as HiddenField).Value.ToString();
responses.responseid = qresponse.responseid;
responses.answerid = Convert.ToDecimal(ansid);
responses.questionid = Convert.ToDecimal(qid);
context.AddToqresponsedetails(responses);
context.SaveChanges();
}
}
return 0;
}