public ActionResult Create([Bind(Include="RowsID,TheatersID,RowNumber,ChairCapacity")] Rows rows,int? last,int? rowCapacity)
        {
            int check = 0;
                if (rows.ChairCapacity == 0 )
                {
                     ViewBag.TheatersID = new SelectList(db.Theaters, "MovieTheatersID", "TheatersName", rows.TheatersID);
                     ViewBag.Message = "you must enter a capacity.";
                     return View(rows);
                }

                if (ModelState.IsValid)
                {
                    db.Rows.Add(rows);
                    check = db.SaveChanges();
                }

                if(check == 1)
                {
                    rows = db.Rows.Where(r => r.TheatersID == rows.TheatersID && r.RowNumber == rows.RowNumber).SingleOrDefault();
                    for (int i = 0; i < rows.ChairCapacity; i++)
                    {
                        HallChairs chair = new HallChairs();
                        chair.ChairNumber = i++;
                        chair.IsSelected = false;
                        chair.RowID = rows.RowsID;

                        if (ModelState.IsValid)
                        {
                            db.HallChairs.Add(chair);
                            db.SaveChanges();
                        }
                    }
                    if (last == rowCapacity)
                    {
                        return RedirectToAction("Index", "TimeScreening", new { id = rows.TheatersID });
                    }
                    else
                    {
                        last++;
                        return RedirectToAction("Create", new { id = rows.TheatersID, rwcpcty = rowCapacity, last = last });
                        //View();
                    }
                }

            ViewBag.Message = "Error by adding the Row's. try again. ";
            ViewBag.TheatersID = new SelectList(db.Theaters, "MovieTheatersID", "TheatersName", rows.TheatersID);
            return View(rows);
        }
        public ActionResult Create( List<MovieTheaters> NewTheaters,List<int> ChairCapacity)
        {
            if (NewTheaters == null || ChairCapacity == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            // flags for check succsss db
            bool checkTheatres = false;
            bool checkRows = false;
            bool checkChairs = false;
            int HomeCinemaID = NewTheaters[0].HomeCinemaID;

            for (int i = 0; i < NewTheaters.Count; i++)
            {
                MovieTheaters movietheaters = NewTheaters[i];
                if (db.Theaters.Where(x => x.TheatersName == movietheaters.TheatersName && x.HomeCinemaID == movietheaters.HomeCinemaID).FirstOrDefault() == movietheaters)
                {
                    ViewBag.ErrorMassage = "You cant add this Theatres again. Choose another name.";
                    ViewBag.HomeCinemaID = new SelectList(db.HomeCinemas.Where(x => x.HomeCinemaID == movietheaters.HomeCinemaID).ToArray(), "HomeCinemaID", "CinemaName", movietheaters.HomeCinemaID);
                    return View(movietheaters);
                }
                else
                {
                    // adding the Theatres for Getting the ID.
                    if (ModelState.IsValid)
                    {
                        db.Theaters.Add(movietheaters);
                        db.SaveChanges();
                        checkTheatres = true;
                    }
                    else
                        checkTheatres = false;

                    for (int f = 0; f < movietheaters.RowAmount; f++)
                    {
                        // adding new row ditels
                        var row = new Rows();
                        row.ChairCapacity = ChairCapacity[(i)];
                        row.RowNumber = (f + 1);
                        row.TheatersID = movietheaters.MovieTheatersID;

                        // add row to db to get the RowID
                        if (ModelState.IsValid)
                        {
                            db.Rows.Add(row);
                            db.SaveChanges();
                            checkRows = true;
                        }
                        else
                            checkRows = false;

                        var Chairs = new List<HallChairs>();
                        for (int j = 0; j < row.ChairCapacity; j++)
                        {
                            var chair = new HallChairs();
                            chair.ChairNumber = (j + 1);
                            chair.IsSelected = false;
                            chair.RowID = row.RowsID;
                            Chairs.Add(chair);
                        }

                        // add chair to db.
                        if (ModelState.IsValid)
                        {
                            db.HallChairs.AddRange(Chairs);
                            db.SaveChanges();
                            checkChairs = true;
                        }
                        else
                            checkChairs = false;
                    }
                }
            }

            if (checkChairs &&  checkTheatres && checkRows)
            {
                return RedirectToAction("Index", "TimeScreening", new { id = HomeCinemaID });
            }
            else
            {
                ViewBag.ErrorMassage = "Try again later";
                ViewBag.HomeCinemaID = new SelectList(db.HomeCinemas, "HomeCinemaID", "CinemaName");
                return View();
            }
        }