예제 #1
0
        // GET: /Products/
        public ActionResult Index(string sortOrder,
                                  String SearchField,
                                  String SearchCondition,
                                  String SearchText,
                                  String Export,
                                  int?PageSize,
                                  int?page,
                                  string command)
        {
            if (command == "Show All")
            {
                SearchField                = null;
                SearchCondition            = null;
                SearchText                 = null;
                Session["SearchField"]     = null;
                Session["SearchCondition"] = null;
                Session["SearchText"]      = null;
            }
            else if (command == "Add New Record")
            {
                return(RedirectToAction("Create"));
            }
            else if (command == "Export")
            {
                Session["Export"] = Export;
            }
            else if (command == "Search" | command == "Page Size")
            {
                if (!string.IsNullOrEmpty(SearchText))
                {
                    Session["SearchField"]     = SearchField;
                    Session["SearchCondition"] = SearchCondition;
                    Session["SearchText"]      = SearchText;
                }
            }
            if (command == "Page Size")
            {
                Session["PageSize"] = PageSize;
            }

            ViewData["SearchFields"]     = GetFields((Session["SearchField"] == null ? "Product Id" : Convert.ToString(Session["SearchField"])));
            ViewData["SearchConditions"] = Library.GetConditions((Session["SearchCondition"] == null ? "Contains" : Convert.ToString(Session["SearchCondition"])));
            ViewData["SearchText"]       = Session["SearchText"];
            ViewData["Exports"]          = Library.GetExports((Session["Export"] == null ? "Pdf" : Convert.ToString(Session["Export"])));
            ViewData["PageSizes"]        = Library.GetPageSizes();

            ViewData["CurrentSort"]       = sortOrder;
            ViewData["ProductIdSortParm"] = sortOrder == "ProductId_asc" ? "ProductId_desc" : "ProductId_asc";
            ViewData["NameSortParm"]      = sortOrder == "Name_asc" ? "Name_desc" : "Name_asc";
            ViewData["QuantitySortParm"]  = sortOrder == "Quantity_asc" ? "Quantity_desc" : "Quantity_asc";
            ViewData["BoxSizeSortParm"]   = sortOrder == "BoxSize_asc" ? "BoxSize_desc" : "BoxSize_asc";
            ViewData["PriceSortParm"]     = sortOrder == "Price_asc" ? "Price_desc" : "Price_asc";

            dtProducts = ProductsData.SelectAll();

            try
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["SearchField"])) & !string.IsNullOrEmpty(Convert.ToString(Session["SearchCondition"])) & !string.IsNullOrEmpty(Convert.ToString(Session["SearchText"])))
                {
                    dtProducts = ProductsData.Search(Convert.ToString(Session["SearchField"]), Convert.ToString(Session["SearchCondition"]), Convert.ToString(Session["SearchText"]));
                }
            }
            catch { }

            var Query = from rowProducts in dtProducts.AsEnumerable()
                        select new Products()
            {
                ProductId  = rowProducts.Field <Int32>("ProductId")
                , Name     = rowProducts.Field <String>("Name")
                , Quantity = rowProducts.Field <Int32>("Quantity")
                , BoxSize  = rowProducts.Field <Int32>("BoxSize")
                , Price    = rowProducts.Field <Decimal>("Price")
            };

            switch (sortOrder)
            {
            case "ProductId_desc":
                Query = Query.OrderByDescending(s => s.ProductId);
                break;

            case "ProductId_asc":
                Query = Query.OrderBy(s => s.ProductId);
                break;

            case "Name_desc":
                Query = Query.OrderByDescending(s => s.Name);
                break;

            case "Name_asc":
                Query = Query.OrderBy(s => s.Name);
                break;

            case "Quantity_desc":
                Query = Query.OrderByDescending(s => s.Quantity);
                break;

            case "Quantity_asc":
                Query = Query.OrderBy(s => s.Quantity);
                break;

            case "BoxSize_desc":
                Query = Query.OrderByDescending(s => s.BoxSize);
                break;

            case "BoxSize_asc":
                Query = Query.OrderBy(s => s.BoxSize);
                break;

            case "Price_desc":
                Query = Query.OrderByDescending(s => s.Price);
                break;

            case "Price_asc":
                Query = Query.OrderBy(s => s.Price);
                break;

            default:      // Name ascending
                Query = Query.OrderBy(s => s.ProductId);
                break;
            }

            if (command == "Export")
            {
                GridView  gv = new GridView();
                DataTable dt = new DataTable();
                dt.Columns.Add("Product Id", typeof(string));
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Quantity", typeof(string));
                dt.Columns.Add("Box Size", typeof(string));
                dt.Columns.Add("Price", typeof(string));
                foreach (var item in Query)
                {
                    dt.Rows.Add(
                        item.ProductId
                        , item.Name
                        , item.Quantity
                        , item.BoxSize
                        , item.Price
                        );
                }
                gv.DataSource = dt;
                gv.DataBind();
                ExportData(Export, gv, dt);
            }

            int pageNumber = (page ?? 1);
            int?pageSZ     = (Convert.ToInt32(Session["PageSize"]) == 0 ? 5 : Convert.ToInt32(Session["PageSize"]));

            return(View(Query.ToPagedList(pageNumber, (pageSZ ?? 5))));
        }