public ActionResult Edit(TournamentViewModel model, int? page, int? count)
        {
            ViewBag.Page = page = page.HasValue && page > 1 ? page : 1;
            ViewBag.Count = count = count.HasValue && count > 1 ? count : __default_count;

            if (model.Document != null && !DocumentHelper.ValidExtension(model.Document.FileName))
            {
                ModelState.AddModelError("Document", "Необходимо загружать документы формата doc (MS Word) или pdf.");
            }

            if (!ModelState.IsValid)
            {
                model.Categories = InitCategories();
                return View(model);
            };

            var tournament = DataAccess.SingleTournaments.FirstOrDefault(t => t.Id == model.Id);
            if (tournament == null)
            {
                model.Categories = InitCategories();
                return View(model);
            }

            tournament.Name = model.Name;
            tournament.StartDate = model.StartDate;
            tournament.EndDate = model.EndDate;
            tournament.NumberOfPlayers = model.NumberOfPlayers;
            tournament.EndOfRegistration = model.EndOfRegistration;
            tournament.Type = model.Type;
            DataAccess.Entry(tournament).Reference(t => t.Category).Load();
            tournament.Category = DataAccess.Categories.Find(model.CategoryId);

            if (model.Document != null)
            {
                var helper = new DocumentHelper(DocumentStoragePath);
                if (!string.IsNullOrEmpty(tournament.DocumentName))
                {
                    helper.Delete(tournament.DocumentName);
                }
                tournament.DocumentName = helper.Upload(model.Document);
            }

            DataAccess.SaveChanges();

            return RedirectToAction("index", new { page = page, count = count });
        }
        public ActionResult Create(TournamentViewModel model, int? page, int? count)
        {
            ViewBag.Page = page = page.HasValue && page > 1 ? page : 1;
            ViewBag.Count = count = count.HasValue && count > 1 ? count : __default_count;

            if (model.Document != null && !DocumentHelper.ValidExtension(model.Document.FileName))
            {
                ModelState.AddModelError("Document", "Необходимо загружать документы формата doc (MS Word) или pdf.");
            }

            if (!ModelState.IsValid)
            {
                model.Categories = InitCategories();
                return View(model);
            }

            var tournament = new SingleTournament()
            {
                Name = model.Name,
                StartDate = model.StartDate,
                EndDate = model.EndDate,
                NumberOfPlayers = model.NumberOfPlayers,
                EndOfRegistration = model.EndOfRegistration,
                Type = model.Type
            };

            var category = DataAccess.Categories.Find(model.CategoryId);
            if (category != null)
            {
                tournament.Category = category;
            }

            if (model.Document != null)
            {
                var helper = new DocumentHelper(DocumentStoragePath);
                if (!string.IsNullOrEmpty(tournament.DocumentName))
                {
                    helper.Delete(tournament.DocumentName);
                }
                tournament.DocumentName = helper.Upload(model.Document);
            }

            tournament.CreateMatches();

            DataAccess.SingleTournaments.Add(tournament);
            DataAccess.SaveChanges();

            CreateConsolationTournaments(tournament);

            return RedirectToAction("index", new { page = page, count = count });
        }