//Adding Sprint
        //GET
        public ActionResult Add()
        {
            var obj = new Sprints();
            //batch list
            Batch b = new Batch();
            b.BatchList = new SelectList(b.GetAllBatches(),"BatchID","BatchDescription");
            obj.Batch = b;

            return View(obj);
        }
 public ActionResult Update(Sprints sprint)
 {
     if (ModelState.IsValid)
     {
         try
         {
             if (sprint != null)
             {
                 try
                 {
                     sprint.BatchID = int.Parse(sprint.SelectedBatch);
                     db.Entry(sprint).State = System.Data.Entity.EntityState.Modified;
                     db.SaveChanges();
                     TempData["previous"] = "Search";
                     TempData["message"] = "The sprint was updated.";
                     return View("~/Views/Shared/Success.cshtml");
                 }
                 catch (Exception e)
                 {
                     TempData["previous"] = "Search";
                     TempData["message"] = "There was an error. Try again or contact administrator.";
                     return View("~/Views/Shared/Error.cshtml");
                 }
             }
             else
             {
                 TempData["previous"] = "Search";
                 TempData["message"] = "There was an error. Try again or contact administrator.";
                 return View("~/Views/Shared/Error.cshtml");
             }
         }
         catch (Exception e)
         {
             TempData["previous"] = "Search";
             TempData["message"] = "There was an error. Try again or contact administrator.";
             return View("~/Views/Shared/Error.cshtml");
         }
     }
     return View();
 }
        public ActionResult Add(Sprints sprint)
        {
            if (!string.IsNullOrEmpty(sprint.SelectedBatch))
            {
                if (ModelState.IsValid)
                {
                    string lessons = Request.Form["LessonsLearned"];
                    //getting batch
                    int BatchID = int.Parse(sprint.SelectedBatch);
                    Batch batch = db.Batch.Find(BatchID);
                    if (batch != null)
                    {
                        try
                        {
                            //obtaining properties
                            sprint.BatchID = BatchID;
                            sprint.RegisteredBy = LoggedUser();
                            sprint.RegisteredDate = DateTime.Now;

                            //adding to database and saving
                            db.Sprints.Add(sprint);
                            db.SaveChanges();

                            //redirecting
                            TempData["previous"] = "Add";
                            TempData["message"] = "The sprint was added.";
                            return View("~/Views/Shared/Success.cshtml");
                        }
                        catch (Exception e)
                        {
                            TempData["previous"] = "Add";
                            TempData["message"] = "There was an error saving sprint.  Try again or contact administrator.";
                            return View("~/Views/Shared/Error.cshtml");
                        }
                    }
                    else
                    {
                        TempData["previous"] = "Add";
                        TempData["message"] = "The site you selected doesn't exist. Try again with another site.";
                        return View("~/Views/Shared/Error.cshtml");
                    }
                }
                else
                {
                    var obj = new Sprints();
                    //batch list
                    Batch b = new Batch();
                    b.BatchList = new SelectList(b.GetAllBatches(), "BatchID", "BatchDescription");
                    obj.Batch = b;
                    return View(obj);
                }
            }
            else
            {
                ModelState.AddModelError("Batch", "You must select a batch.");
                var obj = new Sprints();
                //batch list
                Batch b = new Batch();
                b.BatchList = new SelectList(b.GetAllBatches(), "BatchID", "BatchDescription");
                obj.Batch = b;
                return View(obj);
            }
        }
        public ActionResult Search(Sprints sprint)
        {
            var errors = false;

            //getting site
            if (!string.IsNullOrEmpty(sprint.Batch.Sites.SelectedSite))
            {
                int SiteID = int.Parse(sprint.Batch.Sites.SelectedSite);
                Sites site = db.Sites.Find(SiteID);
                if (site != null)
                {
                    //getting batch
                    if (!string.IsNullOrEmpty(sprint.Batch.SelectedBatch))
                    {
                        int BatchID = int.Parse(sprint.Batch.SelectedBatch);
                        Batch batch = db.Batch.Find(BatchID);
                        //batch-site relation
                        if (batch != null && batch.SiteID == site.SiteID)
                        {
                            var sprints = from s in db.Sprints where s.BatchID == BatchID select s;
                            site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
                            batch.Sites = site;
                            sprint.Batch = batch;
                            sprint.BatchSprints = sprints.ToList();
                            ViewBag.Error = false;
                            return View(sprint);
                        }
                        else
                        {
                            TempData["previous"] = "Search";
                            TempData["message"] = "The batch you selected doesn't exist or doesn't belong to selected site. Try again with another site.";
                            return View("~/Views/Shared/Error.cshtml");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Batch", "You must select a batch.");
                        errors = true;
                    }
                }
                else
                {
                    TempData["previous"] = "Search";
                    TempData["message"] = "The site you selected doesn't exist. Try again with another site.";
                    return View("~/Views/Shared/Error.cshtml");
                }
            }
            else
            {
                ModelState.AddModelError("Site", "You must select a site.");
                ModelState.AddModelError("Batch", "You must select a batch.");
                errors = true;
            }

            if (errors)
            {
                var obj = new Sprints();
                var s = new Sites();
                var b = new Batch();
                s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                b.Sites = s;
                obj.Batch = b;
                return View(obj);
            }
            return View();
        }
 //Search by Site and Batches
 public ActionResult Search()
 {
     var obj = new Sprints();
     var s = new Sites();
     var b = new Batch();
     s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
     b.Sites = s;
     obj.Batch = b;
     return View(obj);
 }
 //List All Sprint
 public ActionResult AllSprints()
 {
     Sprints sprint = new Sprints();
     var sprints = sprint.GetAllSprints();
     if (sprints.Count() > 0)
     {
         return View(sprints.ToList());
     }
     else
     {
         ViewBag.Message = "There are not registered sprints.";
         return View();
     }
 }