コード例 #1
0
        public ActionResult Index(EventViewModel viewModel, string action = null, string delete = null)
        {
            if (action == "new")
            {
                ModelState.Clear();

                if (viewModel.Events == null)
                {
                    viewModel.Events = new List <EventItemModel>();
                }

                viewModel.Events.Add(new EventItemModel());
            }
            else if (action == "motd")
            {
                viewModel.Motd = viewModel.GenerateMotd();
            }
            else if (action == "save")
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        viewModel.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                }
                catch (Exception ex)
                {
                    while (ex != null)
                    {
                        ModelState.AddModelError("", ex.Message);
                        ex = ex.InnerException;
                    }
                }
            }
            else if (!string.IsNullOrWhiteSpace(delete))
            {
                viewModel.Events.RemoveAt(int.Parse(delete));
                ModelState.Clear();
            }

            return(View(viewModel));
        }