コード例 #1
0
ファイル: HomeController.cs プロジェクト: ahrsmnk/gra4
        public async Task <IActionResult> LogActivity(DashboardViewModel viewModel)
        {
            bool valid = true;

            if (!viewModel.SingleEvent &&
                (viewModel.ActivityAmount == null || viewModel.ActivityAmount <= 0))
            {
                valid = false;
                TempData[ActivityErrorMessage] = "Please enter a whole number greater than 0.";
            }
            if (string.IsNullOrWhiteSpace(viewModel.Title) &&
                !string.IsNullOrWhiteSpace(viewModel.Author))
            {
                valid = false;
                TempData[AuthorMissingTitle] = "Please include the Title of the book";
            }
            if (!valid)
            {
                TempData[ModelData] = Newtonsoft.Json.JsonConvert.SerializeObject(viewModel);
            }
            else
            {
                var book = new Domain.Model.Book
                {
                    Author = viewModel.Author,
                    Title  = viewModel.Title
                };
                await _activityService
                .LogActivityAsync(GetActiveUserId(), viewModel.ActivityAmount ?? 1, book);
            }
            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public async Task <IActionResult> LogActivity(DashboardViewModel viewModel)
        {
            bool valid = true;

            if (!viewModel.SingleEvent &&
                (viewModel.ActivityAmount == null || viewModel.ActivityAmount <= 0))
            {
                valid = false;
                TempData[ActivityErrorMessage] = "Please enter a whole number greater than 0.";
            }
            if (!ModelState.IsValid)
            {
                valid = false;
                if (ModelState["Title"].Errors.Count > 0)
                {
                    TempData[TitleErrorMessage] = ModelState["Title"].Errors.First().ErrorMessage;
                }
                if (ModelState["Author"].Errors.Count > 0)
                {
                    TempData[AuthorErrorMessage] = ModelState["Author"].Errors.First().ErrorMessage;
                }
            }
            if (string.IsNullOrWhiteSpace(viewModel.Title) &&
                !string.IsNullOrWhiteSpace(viewModel.Author))
            {
                valid = false;
                TempData[TitleErrorMessage] = "Please include the Title of the book";
            }
            if (!valid)
            {
                TempData[ModelData] = Newtonsoft.Json.JsonConvert.SerializeObject(viewModel);
            }
            else
            {
                var book = new Domain.Model.Book
                {
                    Author = viewModel.Author,
                    Title  = viewModel.Title
                };

                try
                {
                    await _activityService
                    .LogActivityAsync(GetActiveUserId(), viewModel.ActivityAmount ?? 1, book);
                }
                catch (GraException gex)
                {
                    ShowAlertDanger("Could not log activity: ", gex);
                }
            }
            return(RedirectToAction("Index"));
        }