Exemplo n.º 1
0
        }//end Constructor

        public ActionResult Index(string searchString1, string searchString2, string sortOrder)
        {
            DataContext contextForIndex = new DataContext();
            var viewModel =
                from o in contextForIndex.Orders
                join c in contextForIndex.Customers on o.CustomerId equals c.CustomerId
                select new OrderViewModel { Order = o, Customer = c };
            
            if (!String.IsNullOrEmpty(searchString1))
            {
                viewModel = viewModel.Where(s => s.Order.OrderId.ToString().Contains(searchString1));
            }

            if (!String.IsNullOrEmpty(searchString2))
            {
                viewModel = viewModel.Where(s => s.Customer.CustomerName.Contains(searchString2));
            }

            ViewBag.OrderIdSortParm = String.IsNullOrEmpty(sortOrder) ? "id_desc" : "";
            ViewBag.OrderDateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            ViewBag.CustomerNameSortParm = sortOrder == "Customer" ? "name_desc" : "Customer";

            switch (sortOrder)
            {
                case "id_desc":
                    viewModel = viewModel.OrderByDescending(s => s.Order.OrderId);
                    break;
                case "Date":
                    viewModel = viewModel.OrderBy(s => s.Order.OrderDate);
                    break;
                case "date_desc":
                    viewModel = viewModel.OrderByDescending(s => s.Order.OrderDate);
                    break;
                case "Customer":
                    viewModel = viewModel.OrderBy(s => s.Customer.CustomerName);
                    break;
                case "name_desc":
                    viewModel = viewModel.OrderByDescending(s => s.Customer.CustomerName);
                    break;
                default:
                    viewModel = viewModel.OrderBy(s => s.Order.OrderId);
                    break;
            }  

            return View(viewModel);
        }
Exemplo n.º 2
0
        // GET: /Details/5
        public ActionResult Details(int? id)
        {
            var order = orders.GetById(id);
            if (order == null)
            {
                return HttpNotFound();
            }

            DataContext contextForIndex = new DataContext();
            var viewModel =
                from o in contextForIndex.Orders
                join c in contextForIndex.Customers on o.CustomerId equals c.CustomerId
                where o.OrderId == id
                select new OrderViewModel { Order = o, Customer = c };

            return View(viewModel);

        }