예제 #1
0
        void SaveState(ViewModel model)
        {
            //create comma delimited list of product ids
            model.SavedRequested = string.Join(",", model.RequestedProducts.Select(p => p.Id.ToString()).ToArray());

            //Available products = All - Requested
            model.AvailableProducts = Product.GetAllProducts().Except(model.RequestedProducts).ToList();
        }
예제 #2
0
 void RemoveProducts(ViewModel model)
 {
     if (model.RequestedSelected != null)
     {
         model.RequestedProducts.RemoveAll(p => model.RequestedSelected.Contains(p.Id));
         model.RequestedSelected = null;
     }
 }
예제 #3
0
 void AddProducts(ViewModel model)
 {
     if (model.AvailableSelected != null)
     {
         var prods = Product.GetAllProducts().Where(p => model.AvailableSelected.Contains(p.Id));
         model.RequestedProducts.AddRange(prods);
         model.AvailableSelected = null;
     }
 }
예제 #4
0
        void RestoreSavedState(ViewModel model)
        {
            model.RequestedProducts = new List<Product>();

            //get the previously stored items
            if (!string.IsNullOrEmpty(model.SavedRequested))
            {
                string[] prodids = model.SavedRequested.Split(',');
                var prods = Product.GetAllProducts().Where(p => prodids.Contains(p.Id.ToString()));
                model.RequestedProducts.AddRange(prods);
            }
        }
예제 #5
0
 public ActionResult Index(ViewModel model, string add, string remove, string send)
 {
     //Need to clear model state or it will interfere with the updated model
     ModelState.Clear();
     RestoreSavedState(model);
     if (!string.IsNullOrEmpty(add))
         AddProducts(model);
     else if (!string.IsNullOrEmpty(remove))
         RemoveProducts(model);
     else if (!string.IsNullOrEmpty(send))
     {
         Validate(model);
         if (ModelState.IsValid)
             return RedirectToAction("Done");
         //todo: implement SendListToSanta method...
     }
     SaveState(model);
     return View(model);
 }
예제 #6
0
 private void Validate(ViewModel model)
 {
     if (model.RequestedTotal > 400m)
         ModelState.AddModelError("", "Total must be 400 or less");
     if (model.RequestedProducts.Count > 3)
         ModelState.AddModelError("", "Maximum of 3 selections allowed");
     if (string.IsNullOrEmpty(model.SavedRequested))
         ModelState.AddModelError("", "You haven't selected any presents!");
 }
예제 #7
0
 public ActionResult Index()
 {
     ViewModel model = new ViewModel { AvailableProducts = Product.GetAllProducts(), RequestedProducts = new List<Product>() };
     return View(model);
 }