将更新的对象保存在通用列表中

发布于 2024-09-16 15:31:25 字数 1617 浏览 3 评论 0原文

我正在努力更新 List<> 内的业务对象。

我正在使用复选框列表

  1. 当单击复选框列表项时,
  2. 我检查它是否存在,如果是,我将其标记为“脏”(意味着用户取消选中一个项目)
  3. 如果不存在,我将一个新项目添加到列表中(意味着用户单击新项目) 并将其存储在视图状态

    如何使用脏对象更新通用列表? 在表单更新时,我是否 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

  1. When a checkbox list item is clicked
  2. I check if it exists, if yes I mark it as Dirty (means the user unchecks an item)
  3. If not, I add a new item to the list (means the user clicks a new item)
    and store it in view state

    How 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2024-09-23 15:31:28

您无需将其保存回列表中。假设 ftExclusions 是类而不是结构,它始终通过引用传递。这意味着 tempListisExist 都包含对同一对象的引用。并且对象上的任何更改都可以从两个地方看到。

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 both tempList and isExist contain reference to the same object. And any changes on the object will be visible from both places.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文