亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何維護會話中的數據集合?

如何維護會話中的數據集合?

C#
米琪卡哇伊 2022-11-21 22:06:57
為了開具發票,我通過將選定的時間表存儲在瀏覽器會話中并在用戶更新時向該列表添加/刪除條目來跟蹤與發票關聯的時間表條目:GridView 加載所選公司的所有時間表,然后通過更改行樣式和選擇按鈕文本進行指示:private void HighlightInvoiceTimesheets(){    var timesheets = Session["invoiceTimesheets"] as List<Timesheet>;    var invoiceTotalHours = 0;    foreach (var timesheet in timesheets)    {        var tid = timesheet.Id.ToString();        foreach (GridViewRow row in ItemsGrid.Rows)        {            var btn = row.Cells[ItemsGrid.Columns.Count - 1].Controls[0] as LinkButton;            if (ItemsGrid.DataKeys[row.RowIndex].Values["Id"].ToString() == tid)            {                row.CssClass = "success";                btn.Text = "Remove";                int.TryParse(row.Cells[5].Text, out int timesheetHours);                invoiceTotalHours += timesheetHours;            }        }    }    Session["invoiceTotalHours"] = invoiceTotalHours;    BillableHoursLabel.Text = invoiceTotalHours.ToString();}當用戶在 GridView 中“選擇”一個項目時,它會在 Session 的集合中添加或刪除該項目,并相應地更新 GridView:protected void ItemsGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e){    // Get the list of timesheets associated with the invoice.    var list = (Session["invoiceTimesheets"] == null) ? new List<Timesheet>() : Session["invoiceTimesheets"] as List<Timesheet>;    // Get the selected timesheet.    int.TryParse(ItemsGrid.DataKeys[e.NewSelectedIndex].Values["Id"].ToString(), out int timesheetId);    var timesheet = timesheetService.GetClearTimesheet(timesheetId);    // Get the select button to update its text.    var btn = ItemsGrid.Rows[e.NewSelectedIndex].Cells[ItemsGrid.Columns.Count - 1].Controls[0] as LinkButton;    // Get the total hours billable for the invoice based on the total hours of the timesheets.    var invoiceTotalHours = (Session["invoiceTotalHours"] == null) ? 0 : int.Parse(Session["invoiceTotalHours"].ToString());}這沒有任何錯誤,但我很困惑為什么list.Remove(timesheet);實際上不更新內存中的列表。因此,會話中的集合不會更新,所做的更改也不會反映在數據庫中。
查看完整描述

1 回答

?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

這是因為您要刪除的時間表與您從中獲得的時間表不是同一個對象


var timesheet = timesheetService.GetClearTimesheet(timesheetId);

而不是這個:


if (list.Find(x => x.Id == timesheetId) != null)

{

    // The list contains the selected item, remove it and indicate removed.

    list.Remove(timesheet);

做這個:


var timeSheetSession=list.FirstOrDefault(x => x.Id == timesheetId);

if(timeSheetSession!=null)  list.Remove(timeSheetSession);

這是偽代碼,我沒有測試過。


查看完整回答
反對 回復 2022-11-21
  • 1 回答
  • 0 關注
  • 101 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號