如何在页面重新访问或重新加载时正确清除GridViewExtension数据?

时间:2016-06-30 02:53:10

标签: javascript c# asp.net-mvc gridview devexpress-mvc

背景

我有一个不完整的项目建立在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. 每次第一次访问,重新访问&amp;在Winner页面上重新加载,gridview内容必须为空。
  2. 用户提供某个客户ID后,gridview将填充单位表中的某些单位数据,当用户接受重新加载/关闭页面确认消息时,其中的更改会立即丢失。
  3. 任何类型的答案或建议都会受到赞赏。

0 个答案:

没有答案