public ActionResult EditRows(Order editedOrder)
        {
            // Get the grid and database (northwind) models
            var gridModel = new OrdersJqGridModel();
            var northWindModel = new NorthwindDataContext();

            // If we are in "Edit" mode
            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                // Get the data from and find the Order corresponding to the edited row
                Order order = (from o in northWindModel.Orders
                               where o.OrderID == editedOrder.OrderID
                               select o).First<Order>();

                // update the Order information
                order.OrderDate = editedOrder.OrderDate;
                order.CustomerID = editedOrder.CustomerID;
                order.Freight = editedOrder.Freight;
                order.ShipName = editedOrder.ShipName;

                northWindModel.SubmitChanges();
            }
            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.AddRow)
            {
                // since we are adding a new Order, create a new istance
                Order order = new Order();
                // set the new Order information
                order.OrderID = (from o in northWindModel.Orders
                                 select o)
                                .Max<Order>(o => o.OrderID) + 1;
                order.OrderDate = editedOrder.OrderDate;
                order.CustomerID = editedOrder.CustomerID;
                order.Freight = editedOrder.Freight;
                order.ShipName = editedOrder.ShipName;

                northWindModel.Orders.InsertOnSubmit(order);
                northWindModel.SubmitChanges();
            }
            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {
                Order order = ( from o in northWindModel.Orders
                                where o.OrderID == editedOrder.OrderID
                                select o)
                               .First<Order>();

                // delete the record
                northWindModel.Orders.DeleteOnSubmit(order);
                northWindModel.SubmitChanges();
            }

            return RedirectToAction("GridDemo", "Grid");
        }
        // This method is called when the grid requests data
        public JsonResult SearchGridDataRequested()
        {
            // Get both the grid Model and the data Model
            // The data model in our case is an autogenerated linq2sql database based on Northwind.
            var gridModel = new OrdersJqGridModel();
            var northWindModel = new NorthwindDataContext();

            // customize the default Orders grid model with our custom settings
            SetUpGrid(gridModel.OrdersGrid);

            // return the result of the DataBind method, passing the datasource as a parameter
            // jqGrid for ASP.NET MVC automatically takes care of paging, sorting, filtering/searching, etc
            return gridModel.OrdersGrid.DataBind(northWindModel.Orders);
        }
        private void SetUpCustomerIDSearchDropDown(JQGrid ordersGrid)
        {
            // setup the grid search criteria for the columns
            JQGridColumn customersColumn = ordersGrid.Columns.Find(c => c.DataField == "CustomerID");
            customersColumn.Searchable = true;

            // DataType must be set in order to use searching
            customersColumn.DataType = typeof(string);
            customersColumn.SearchToolBarOperation = SearchOperation.IsEqualTo;
            customersColumn.SearchType = SearchType.DropDown;

            // Populate the search dropdown only on initial request, in order to optimize performance
            if (ordersGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
            {
                var northWindModel = new NorthwindDataContext();
                var searchList = from customers in northWindModel.Customers
                                 select new SelectListItem
                                 {
                                     Text = customers.CustomerID,
                                     Value = customers.CustomerID
                                 };

                customersColumn.SearchList = searchList.ToList<SelectListItem>();
                customersColumn.SearchList.Insert(0, new SelectListItem { Text = "All", Value = "" });
            }
        }
        private void SetUpCustomerIDEditDropDown(JQGrid ordersGrid)
        {
            // setup the grid search criteria for the columns
            JQGridColumn customersColumn = ordersGrid.Columns.Find(c => c.DataField == "CustomerID");
            customersColumn.Editable = true;
            customersColumn.EditType = EditType.DropDown;

            // Populate the search dropdown only on initial request, in order to optimize performance
            if (ordersGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
            {
                var northWindModel = new NorthwindDataContext();
                var editList = from customers in northWindModel.Customers
                               select new SelectListItem
                               {
                                   Text = customers.CustomerID,
                                   Value = customers.CustomerID
                               };

                customersColumn.EditList = editList.ToList<SelectListItem>();
            }
        }