2

Background

I have an incomplete project built on MVC 5, EF 6 DB First & DevExpress extensions with following code spec (all original non-English variable names changed & entire code simplified to comply MCVE):

Model (Namespace: 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
}

Controller

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

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

Partial View (_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();
}

Partial View (_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>
}

All gridview changes require sufficent privileges (i.e. admin/supervisor).

Problem Statement

I want anyone help finding out where and how proper routine codes to empty gridview data must be attached on controller method(s) to give expected results. As I tried so far, the gridview still maintaining its previous state given from session variable when Winner page revisited or reloaded (immediate first visit after login worked because all session variables are empty, thus no data was populated to gridview).

Additionally, I want to show JS confirm message when user trying to close/reload Winner page while some/all gridview data are being checked.

Expected Results

  1. For every first visit, revisit & reload on Winner page, the gridview content must empty.
  2. After a user provides certain customer ID, the gridview will populated with some unit data from unit table, where changes inside it immediately lost when user accepts reloading/closing page confirm message.

Any kind of answers or suggestions will appreciated.

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61

0 Answers0