public ActionResult Edit(DiscountCodeEdit discountCodeEdit)
        {
            var discountCode = _discountCodeService.FindDiscountCode(discountCodeEdit.Id);

              if (!ModelState.IsValid) return View(BuildEditModel(discountCodeEdit, discountCode));

              Mapper.Map(discountCodeEdit, discountCode);
              UpdateTargets(discountCode, discountCodeEdit);
              _discountCodeService.Update(discountCode);
              return RedirectToRoute("admin-discount-codes");
        }
 public ActionResult Create(DiscountCodeEdit discountCodeEdit)
 {
     var discountCode = Mapper.Map<DiscountCode>(discountCodeEdit);
       if (ModelState.IsValid)
       {
     UpdateTargets(discountCode, discountCodeEdit);
     _discountCodeService.Add(discountCode);
     return RedirectToRoute("admin-discount-codes");
       }
       return View(BuildEditModel(discountCodeEdit, discountCode));
 }
        private void UpdateTargets(DiscountCode discountCode, DiscountCodeEdit discountCodeEdit)
        {
            if (discountCode.Tours == null)
            discountCode.Tours = new List<Tour>();
              discountCode.Tours.Clear();
              if (!discountCode.IsGlobal && discountCodeEdit.TourId != null)
            discountCode.Tours =
              discountCodeEdit.TourId.Select(i => _tourQueryService.FindTour(Convert.ToInt32(i))).ToList();

              if (discountCode.Products == null)
            discountCode.Products = new List<Product>();
              discountCode.Products.Clear();
              if (!discountCode.IsGlobal && discountCodeEdit.ProductId != null)
            discountCode.Products =
              discountCodeEdit.ProductId.Select(i => _productQueryService.FindProduct(Convert.ToInt32(i))).ToList();

              if (discountCode.ProductVariants == null)
            discountCode.ProductVariants = new List<ProductVariant>();
              discountCode.ProductVariants.Clear();
              if (!discountCode.IsGlobal && discountCodeEdit.ProductVariantId != null)
            discountCode.ProductVariants =
              discountCodeEdit.ProductVariantId.Select(i => _productQueryService.FindProductVariant(Convert.ToInt32(i))).ToList();
        }
        private DiscountCodeEdit BuildEditModel(DiscountCodeEdit discountCodeEdit, DiscountCode discountCode)
        {
            var tours = _tourQueryService.Tours().ToList();
              discountCodeEdit.TourItems =
            tours.Select(
              t =>
            new SelectListItem
            {
              Text = string.Format("{0} - {1}", t.Code, t.Name),
              Value = t.Id.ToString(CultureInfo.InvariantCulture)
            });

              var firstTour = tours.FirstOrDefault();
              if (firstTour != null && firstTour.Departures.Any())
              {
            discountCodeEdit.ProductItems =
              firstTour.Departures.Select(d => d.Product)
            .Select(
              p =>
                new SelectListItem
                {
                  Text = string.Format("{0} - {1}", p.Departure.Code, p.Name),
                  Value = p.Id.ToString(CultureInfo.InvariantCulture)
                });

            var firstProduct = firstTour.Departures.First().Product;
            if (firstProduct != null)
            {
              discountCodeEdit.ProductVariantItems =
            firstProduct.ProductVariants.Select(
              pv => new SelectListItem
              {
                Text = string.Format("{0} - {1}", pv.Product.Departure.Code, pv.DisplayName),
                Value = pv.Id.ToString(CultureInfo.InvariantCulture)
              });
            }
              }

              if (discountCode.Tours != null)
              {
            discountCodeEdit.ToursJson =
              new JavaScriptSerializer().Serialize(
            discountCode.Tours.Select(
              t => new { id = t.Id, name = string.Format("{0} - {1}", t.Code, t.Name), targetType = "TourId" }));
              }

              if (discountCode.Products != null)
              {
            discountCodeEdit.ProductsJson =
              new JavaScriptSerializer().Serialize(
            discountCode.Products.Select(
              p =>
                new { id = p.Id, name = string.Format("{0} - {1}", p.Departure.Code, p.Name), targetType = "ProductId" }));
              }

              if (discountCode.ProductVariants != null)
              {
            discountCodeEdit.ProductVariantsJson =
              new JavaScriptSerializer().Serialize(
            discountCode.ProductVariants.Select(
              v =>
                new
                {
                  id = v.Id,
                  name = string.Format("{0} - {1}", v.Product.Departure.Code, v.DisplayName),
                  targetType = "ProductVariantId"
                }));
              }
              return discountCodeEdit;
        }