コード例 #1
0
        public ActionResult CreatePromotion(FormCollection collection)
        {
            int promotedPass = Convert.ToInt32(collection["Passes"]);

            string    promoDesc;
            double    discount;
            int       extraPasses;
            DateTime  promoEnd;
            Promotion promo;

            // Check first if pass already has one
            if (db.CheckIfPromoExist(promotedPass))
            {
                if (db.CheckIfActivePromoExist(promotedPass))
                {
                    TempData["Message"] = "This class pass already has a promotion assigned.";
                    return(RedirectToAction("CreatePromotion"));
                }
                else
                {
                    promoDesc = collection["PromotionDescription"];


                    try
                    {
                        discount = Convert.ToDouble(collection["discount"]) / 100;
                    }
                    catch
                    {
                        discount = 0;
                    }

                    try
                    {
                        extraPasses = Int32.Parse(collection["extra_passes"]);
                    }
                    catch
                    {
                        extraPasses = 0;
                    }


                    promoEnd = Convert.ToDateTime(collection["promoEnd"]);

                    promo = new Promotion();


                    promo.Promo_Desc  = promoDesc;
                    promo.Discount    = Convert.ToDecimal(discount);
                    promo.Num_Classes = extraPasses;
                    promo.Promo_End   = promoEnd;
                    promo.Pass_Id     = promotedPass;


                    // validation (one or the other type of promo but not both
                    if (discount > 0 && extraPasses > 0)
                    {
                        TempData["Message"] = "The promotion can have either a discount or extra classes but not both.";

                        //ViewBag.StickyEmail = email;
                        return(RedirectToAction("CreatePromotion"));
                    }
                    // validation (one or the other type of promo but not both
                    if (discount == 0 && extraPasses == 0)
                    {
                        TempData["Message"] = "The promotion needs to have either a discount or extra classes but not both.";

                        return(RedirectToAction("CreatePromotion"));
                    }


                    db.CreatePromotion(promo);
                    return(RedirectToAction("PromotionList"));
                }
            }



            promoDesc = collection["PromotionDescription"];

            try
            {
                discount = Convert.ToDouble(collection["discount"]) / 100;
            }
            catch
            {
                discount = 0;
            }

            try
            {
                extraPasses = Int32.Parse(collection["extra_passes"]);
            }
            catch
            {
                extraPasses = 0;
            }


            promoEnd = Convert.ToDateTime(collection["promoEnd"]);

            promo = new Promotion();


            promo.Promo_Desc  = promoDesc;
            promo.Discount    = Convert.ToDecimal(discount);
            promo.Num_Classes = extraPasses;
            promo.Promo_End   = promoEnd;
            promo.Pass_Id     = promotedPass;


            // validation (one or the other type of promo but not both
            if (discount > 0 && extraPasses > 0)
            {
                TempData["Message"] = "The promotion can have either a discount or extra classes but not both.";

                //ViewBag.StickyEmail = email;
                return(RedirectToAction("CreatePromotion"));
            }
            // validation (one or the other type of promo but not both
            if (discount == 0 && extraPasses == 0)
            {
                TempData["Message"] = "The promotion needs to have either a discount or extra classes but not both.";

                return(RedirectToAction("CreatePromotion"));
            }


            db.CreatePromotion(promo);
            return(RedirectToAction("PromotionList"));
        }