簡體   English   中英

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

[英]How to maintain collections of data in session?

為了開發票,我通過將選定的時間表存儲在瀏覽器會話中並在用戶更新時將條目添加/刪除到該列表中來跟蹤與發票關聯的時間表條目:

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());

    if (list.Find(x => x.Id == timesheetId) != null)
    {
        // The list contains the selected item, remove it and indicate removed.
        list.Remove(timesheet);
        ItemsGrid.Rows[e.NewSelectedIndex].CssClass = "";
        btn.Text = "Select";
        int.TryParse(Session["invoiceTotalHours"].ToString(), out invoiceTotalHours);
        invoiceTotalHours -= timesheet.BillableHours;
    }
    else
    {
        // The list doesn't contain the selected item, add it and indicate added.
        list.Add(timesheet);
        ItemsGrid.Rows[e.NewSelectedIndex].CssClass = "success";
        btn.Text = "Remove";
        int.TryParse(Session["invoiceTotalHours"].ToString(), out invoiceTotalHours);
        invoiceTotalHours += timesheet.BillableHours;
    }

    BillableHoursLabel.Text = invoiceTotalHours.ToString();
    // Update the collection in the session.
    Session["invoiceTimesheets"] = list;
}

這可以正常工作,但是我很困惑為什么list.Remove(timesheet); 實際上不會更新內存中的列表。

結果,會話中的集合不會得到更新,所做的更改也不會反映在數據庫中。

這是因為您要刪除的時間表與您從中獲取的時間表不同

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);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM