コード例 #1
0
        public IActionResult New(int id)
        {
            var user = this.User.Identity.Name;

            var adInput = new AdInputViewModel {
                ApplicationUser = user, SentOn = DateTime.UtcNow
            };

            return(this.View(adInput));
        }
コード例 #2
0
        public async Task CreateAsync(AdInputViewModel input, string userId)
        {
            var ad = new Ad()
            {
                SentOn            = DateTime.UtcNow,
                Text              = input.Text,
                ApplicationUserId = userId,
            };

            await this.adsRepo.AddAsync(ad);

            await this.adsRepo.SaveChangesAsync();
        }
コード例 #3
0
        public async Task <IActionResult> New(AdInputViewModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            try
            {
                await this.adsService.CreateAsync(input, user.Id);
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);

                return(this.View(input));
            }

            this.TempData["Message"] = "Ad added successfully.";

            return(this.RedirectToAction("MyPets", "Pets"));
        }