Ejemplo n.º 1
0
        public ActionResult Subscribe([Bind(Include = "SubscriberEmail")] SubscribersModel subscribersModel)
        {
            var ReturnUrl = Request.Form["ReturnUrl"];

            if (string.IsNullOrEmpty(ReturnUrl))
            {
                ReturnUrl = "Index";
            }

            TempData["SubsAlertDiv"] = "SubsAlertDiv";
            if (ModelState.IsValid)
            {
                subscribersModel.Status = 1;

                if (db.Subcribers.Any(s => s.SubscriberEmail == subscribersModel.SubscriberEmail && s.Status != 0))
                {
                    TempData["ErrorMessage"] = "Already subscribed.";
                    return(Redirect(ReturnUrl));
                }
                else if (db.Subcribers.Any(s => s.SubscriberEmail == subscribersModel.SubscriberEmail && s.Status == 0))
                {
                    //Update status
                    if (AppFunctions.UpdateSubcriberStatus(subscribersModel.SubscriberEmail, 1))
                    {
                        TempData["SuccessMessage"] = "Subscription successfull.";
                        return(Redirect(ReturnUrl));
                    }
                    TempData["ErrorMessage"] = "Failed to re-subscribe.";
                    return(Redirect(ReturnUrl));
                }

                db.Subcribers.Add(subscribersModel);
                db.SaveChanges();
                TempData["SuccessMessage"] = "Subscription successfull.";

                if (!string.IsNullOrEmpty(ReturnUrl))
                {
                    return(Redirect(ReturnUrl));
                }
                return(RedirectToAction("Index", "Home"));
            }

            TempData["ErrorMessage"] = "Opps!an  error occured.";
            if (!string.IsNullOrEmpty(ReturnUrl))
            {
                return(Redirect(ReturnUrl));
            }
            return(RedirectToAction("Index", "Home"));
        }