2

背景

我有一个基于 MVC 5、EF 6 DB First 和 DevExpress 扩展的不完整项目,具有以下代码规范(所有原始非英语变量名称已更改,整个代码已简化以符合 MCVE):

模型(命名空间ProjectName.Models:)

public class DBContext : DbContext
{
    public DBContext : base("ConnectionStringName")
    {
        public DbSet<WinnerTable> Winners { get; set; }
        public DbSet<UnitTable> Units { get; set; }
        public DbSet<CustomerTable> Customers { get; set; }
    }
}

[Table("WinnerTable")]
public class WinnerModel : IWinnerRepository
{
    public String WinnerID { get; set; }
    public String CustomerID { get; set; }
    public String CustomerName { get; set; }
    public String TranDate { get; set; }

    public List<UnitModel> UnitList { get; set; }

    public List<UnitModel> GetUnitList(String sessionID, DateTime tranDate)
    {
        // query to unit list
        using (var DB = new DBContext())
        {
            var query = (from unit in DB.Units
                         where unit.SessionID == sessionID && unit.TranDate = tranDate
                         select new UnitModel() 
                         {
                              // unit table to unit model definition
                         }).ToList();
            return query;
        }
    }
}

[Table("UnitTable")]
public class UnitModel
{
    public String UnitID { get; set; }
    public String UnitName { get; set; }
    // other definitions
}

控制器

using ProjectName.Models;

[RoutePrefix("Input")]
public class InputController : Controller
{
    [HttpGet]
    public ActionResult Winner()
    {
        WinnerModel model = new WinnerModel()
        {
            // default values on first visit/reload page
            TranDate = DateTime.Now.Date,
            UnitList = new List<UnitModel>(); // list declaration
        }
        return View(model);
    }

    public PartialViewResult CustomerData(String customerId, String sessionId, DateTime tranDate, WinnerModel model)
    {
        if (DevExpressHelper.IsCallback && !String.IsNullOrEmpty(customerId))
        {
            Session["CustomerID"] = customerId;
            Session["SessionID"] = sessionId;
            Session["TranDate"] = Convert.ToDateTime(tranDate);

            using (var DB = new DBContext())
            {
                var query = DB.Customers.Where(c => c.CustomerID == customerId).FirstOrDefault();
                // model property assignments
            }
        }

        return PartialView("_CustomerData", model);
    }

    public PartialViewResult ShowItemsGrid(WinnerModel model)
    {
        String customerId = (Session["CustomerId"] ?? String.Empty).ToString();
        String sessionId = (Session["SessionId"] ?? String.Empty).ToString();
        String lastCustomer = (Session["LastCustomer"] ?? String.Empty).ToString();
        DateTime tranDate = Convert.ToDateTime(Session["TranDate"] ?? DateTime.Now.Date);

        using (var DB = new DBContext())
        {
            model.CustomerId = customerId;
            model.SessionId = sessionId;
            model.TranDate = tranDate;

            model.UnitList = model.GetUnitList(sessionId, tranDate);

            if (model.UnitList == null || model.UnitList.Count == 0)
            {
                model.UnitList = new List<UnitModel>();
            }

            Session["LastCustomer"] = lastCustomer;

            return PartialView("_GridView", model);
        }
    }
}

查看 (Winner.cshtml)

@using ProjectName.Models

@model WinnerModel

@Html.EnableUnobtrusiveJavascript()

<script type="text/javascript">
   var customer = null;

   function initializeGrid()
   {
       ItemsGrid.PerformCallback(); // routine check if customer name exists
   }

   function comboChanged(s, e) {
       customer = s.GetValue();
       CustomerDataPanel.PerformCallback(); // callback to fill customer data for partial view & load units into gridview
   }

   // callback to insert values into session variable
   function customerBeginCallback(s, e) {
       e.customArgs["customerId"] = customer;
       e.customArgs["sessionId"] = SessionId.GetValue();
       e.customArgs["tranDate"] = TranDate.GetValue();
   }

   function customerEndCallback(s, e) {
       ItemsGrid.PerformCallback();
   }

   // count checked data inside gridview
   // this may be asked on other context and doesn't matter for this one
   function countUnits(buttonName, url)
   {
       // other code
   }
</script>

@using (Html.BeginForm("Winner", "Input", FormMethod.Post))
{
    Html.DevExpress().TextBoxFor(m => m.SessionId, TextBoxSettings).GetHtml();

    Html.DevExpress().DateEditFor(m => m.TranDate, DateEditSettings).GetHtml(); 

    // this combobox has client-side event SelectedIndexChanged = "comboChanged"
    // GetCustomers method just populate customers data into combobox and unrelated to this problem
    Html.DevExpress().ComboBoxFor(m => m.CustomerId, ComboBoxSettings).BindList(ProjectName.Providers.GetCustomers()).GetHtml();

    Html.RenderPartial("_CustomerData", Model); // DX callback panel

    Html.RenderPartial("_GridView", Model);

    // button to count all checked values inside gridview
    Html.DevExpress().Button(CountButtonSettings).GetHtml();

    Html.DevExpress().LabelFor(m => m.TotalPrice, PriceLabelSettings).GetHtml();

    // button for submit & reset form here
    Html.DevExpress().Button(SubmitButtonSettings).GetHtml();
    Html.DevExpress().Button(ResetButtonSettings).GetHtml();
}

部分视图 (_CustomerData.cshtml)

@using ProjectName.Models

@model WinnerModel

@{
    // MVC DX callback panel for customer details
    // Name = CustomerDataPanel
    // CallbackRouteValues: Controller = Input, Action = CustomerData 
    // ClientSideEvents.BeginCallback = customerBeginCallback
    // ClientSideEvents.EndCallback = customerEndCallback

    Html.DevExpress().CallbackPanel(CallbackPanelSettings).GetHtml();
}

部分视图 (_GridView.cshtml)

@using ProjectName.Models

@model WinnerModel

@{
    // MVC DX GridView with row selection checkboxes
    // The gridview column structure is exactly same as UnitModel has
    // Name = ItemsGrid
    // CallbackRouteValues: Controller = Input, Action = ShowItemsGrid
    // ClientSideEvents.Init = initializeGrid

    GridViewExtension grid = Html.DevExpress().GridView(GridViewSettings); 
    grid.Bind(Model.UnitList).GetHtml(); // grid bound to List<UnitModel>
}

所有 gridview 更改都需要足够的权限(即管理员/主管)。

问题陈述

我希望任何人帮助找出必须在控制器方法上附加用于清空 gridview 数据的适当例程代码的位置和方式,以提供预期的结果。正如我到目前为止所尝试的那样,当Winner重新访问或重新加载页面时,gridview 仍然保持其先前从会话变量给出的状态(登录后立即第一次访问有效,因为所有会话变量都是空的,因此没有数据填充到 gridview)。

此外,我想在用户尝试关闭/重新加载Winner页面时显示 JS 确认消息,同时检查一些/所有 gridview 数据。

预期成绩

  1. 对于每次第一次访问、重新访问和重新加载Winner页面,gridview 内容必须为空。
  2. 用户提供特定客户 ID 后,gridview 将填充单元表中的一些单元数据,当用户接受重新加载/关闭页面确认消息时,其中的更改立即丢失。

任何类型的答案或建议将不胜感激。

4

0 回答 0