如何维护会话中的数据收集?

时间:2019-04-01 10:48:02

标签: c# asp.net

出于开具发票的目的,我通过将选定的时间表存储在浏览器会话中并在用户更新时在该列表中添加/删除条目来跟踪与发票相关的时间表条目:

GridView会加载选定公司的所有时间表,然后通过更改行样式和选择按钮文本进行指示:

DropCapText(
    loremIpsumText,
    dropCap: DropCap(
    width: 100,
    height: 100,
    child: Image.network(
        'https://www.codemate.com/wp-content/uploads/2017/09/flutter-logo.png')
    ),
),

当用户在GridView中“选择”一个项目时,它会在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();
}

这可以正常工作,但是我很困惑为什么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; } 并没有真正更新内存中的列表。

因此,会话中的集合不会得到更新,所做的更改也不会反映在数据库上。

1 个答案:

答案 0 :(得分:2)

这是因为您要删除的时间表与您获取的时间表不同。

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

这是伪代码,我没有对其进行测试。