最后一项仅添加到数据库中
本文关键字:添加 数据库 一项 最后 | 更新日期: 2023-09-27 17:58:23
我试图向列表中添加一些控件,但由于某种原因,它只将最后一个视图保存到数据库中。
public List<tblPortalIandEValue> createIandERecord(Guid _userId, string fieldName, string fieldValue)
{
List<tblPortalIandEValue> _iandevalues = new List<tblPortalIandEValue>();
_iandevalues.Add(new tblPortalIandEValue { userId = _userId, field_name = fieldName, field_value = fieldValue });
return _iandevalues;
}
我认为我的问题在于我的保存,因为我称这个列表为add new,然后返回它认为我应该返回记录而不是
protected void btnSave_Click(object sender, EventArgs e)
{
List<tblPortalIandEValue> _iandevalues = new List<tblPortalIandEValue>();
_customerId = Guid.NewGuid();
_iandevalues=_dal.createIandERecord(_customerId , "mortagecic", mortagecic.Value.ToString());
_iandevalues = _dal.createIandERecord(_customerId, "gascic", gascic.Value.ToString());
_iandevalues = _dal.createIandERecord(_customerId, "electricitycic", electricitycic.Value.ToString());
_iandevalues.ForEach(n =>_dal.portalEntities.tblPortalIandEValues.Add(n));
_dal.portalEntities.SaveChanges();
}
您的问题在于createIandERecord方法,因为您每次都在续订列表。将您的代码更改为:
public tblPortalIandEValue createIandERecord(Guid _userId, string fieldName, string fieldValue)
{
return new tblPortalIandEValue { userId = _userId, field_name = fieldName, field_value = fieldValue });
}
protected void btnSave_Click(object sender, EventArgs e)
{
List<tblPortalIandEValue> _iandevalues = new List<tblPortalIandEValue>();
_customerId = Guid.NewGuid();
_iandevalues.Add(_dal.createIandERecord(_customerId, "mortagecic", mortagecic.Value.ToString()));
_iandevalues.Add(_dal.createIandERecord(_customerId, "gascic", gascic.Value.ToString()));
_iandevalues.Add(_dal.createIandERecord(_customerId, "electricitycic", electricitycic.Value.ToString()));
_iandevalues.ForEach(n => _dal.portalEntities.tblPortalIandEValues.Add(n));
_dal.portalEntities.SaveChanges();
}