将更新的对象保存在通用列表中
我正在努力更新 List<> 内的业务对象。
我正在使用复选框列表
- 当单击复选框列表项时,
- 我检查它是否存在,如果是,我将其标记为“脏”(意味着用户取消选中一个项目)
如果不存在,我将一个新项目添加到列表中(意味着用户单击新项目) 并将其存储在视图状态
如何使用脏对象更新通用列表? 在表单更新时,我是否 foreach 并制作单独的脏对象和新对象列表以发送到数据库层,或者您会推荐任何其他方式吗?
这是我的代码,对于错误的逻辑感到抱歉,我只是一个初学者 =(
protected void cblExclusions_SelectedIndexChanged(object sender, EventArgs e)
{
if (cblExclusions.SelectedIndex != -1)
{
ftExclusions myExclusion=new ftExclusions(); // Business object
ftExclusionsList myExclusionList=new ftExclusionsList(); // Business Obj. List
int excId = Convert.ToInt32(cblExclusions.SelectedValue.ToString()); // value of checkbox list item which is clicked
ftExclusionsList tempList = (ftExclusionsList)ViewState["ftExclusionList"];
ftExclusions isExist =tempList.Find(delegate(ftExclusions tmpExclusion)
{
return (tmpExclusion.excluId == excId);
});
if (isExist != null)
{
isExist.isDirtyExclusion(); // Mark as dirty
tempList. // stuck here I don't grasp how to save this back to the list
}
else
{
myExclusion = new ftExclusions();
myExclusion.excluId = excId;
myExclusion.fTrtID = Convert.ToInt32(lblTreatyNo.Text);
myExclusion.ftExcluId = -1; //new record
myExclusion.isNewExclusion(); // Mark as new
tempList.Add(myExclusion);
}
ViewState["ftExclusionList"] = tempList;
}
}
I am trying hard to update a business object inside a List<>.
I am using a checkbox list
- When a checkbox list item is clicked
- I check if it exists, if yes I mark it as Dirty (means the user unchecks an item)
If not, I add a new item to the list (means the user clicks a new item)
and store it in view stateHow do I update the Generic List with the Dirty Object ?
On form update, do I foreach and make separate lists of dirty and new objects to send to DB layer or would you recommend any other way ?
Here is my code, sorry about the bad logic I am just a starter =(
protected void cblExclusions_SelectedIndexChanged(object sender, EventArgs e)
{
if (cblExclusions.SelectedIndex != -1)
{
ftExclusions myExclusion=new ftExclusions(); // Business object
ftExclusionsList myExclusionList=new ftExclusionsList(); // Business Obj. List
int excId = Convert.ToInt32(cblExclusions.SelectedValue.ToString()); // value of checkbox list item which is clicked
ftExclusionsList tempList = (ftExclusionsList)ViewState["ftExclusionList"];
ftExclusions isExist =tempList.Find(delegate(ftExclusions tmpExclusion)
{
return (tmpExclusion.excluId == excId);
});
if (isExist != null)
{
isExist.isDirtyExclusion(); // Mark as dirty
tempList. // stuck here I don't grasp how to save this back to the list
}
else
{
myExclusion = new ftExclusions();
myExclusion.excluId = excId;
myExclusion.fTrtID = Convert.ToInt32(lblTreatyNo.Text);
myExclusion.ftExcluId = -1; //new record
myExclusion.isNewExclusion(); // Mark as new
tempList.Add(myExclusion);
}
ViewState["ftExclusionList"] = tempList;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无需将其保存回列表中。假设 ftExclusions 是类而不是结构,它始终通过引用传递。这意味着
tempList
和isExist
都包含对同一对象的引用。并且对象上的任何更改都可以从两个地方看到。You do no need to save it back to the list. Assuming
ftExclusions
is class and not struct, it is always passed by reference. Meaning that bothtempList
andisExist
contain reference to the same object. And any changes on the object will be visible from both places.