示例#1
0
        public async Task <IActionResult> Create(ActVoteViewModel view)
        {
            if (ModelState.IsValid)
            {
                var path = string.Empty;

                if (view.ImageFile != null && view.ImageFile.Length > 0)
                {
                    var guid = Guid.NewGuid().ToString();
                    var file = $"{guid}.jpg";

                    path = Path.Combine(
                        Directory.GetCurrentDirectory(),
                        "wwwroot\\images\\Actvot",
                        file);

                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await view.ImageFile.CopyToAsync(stream);
                    }

                    path = $"~/images/Actvot/{file}";
                }
                var Actvote = this.ToActvote(view, path);
                Actvote.user = await this.userHelper.GetUserByEmailAsync(this.User.Identity.Name);

                await this.actVoteRepository.CreateAsync(Actvote);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(view));
        }
示例#2
0
 private ActVote ToActvote(ActVoteViewModel view, string path)
 {
     return(new ActVote
     {
         Id = view.Id,
         ImageUrl = path,
         Name = view.Name,
         Actstar = view.Actstar,
         Endstar = view.Endstar,
         Description = view.Description,
         user = view.user
     });
 }
示例#3
0
        public async Task <IActionResult> Edit(ActVoteViewModel view)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var path = view.ImageUrl;
                    if (view.ImageFile != null && view.ImageFile.Length > 0)
                    {
                        var guid = Guid.NewGuid().ToString();
                        var file = $"{guid}.jpg";

                        path = Path.Combine(
                            Directory.GetCurrentDirectory(),
                            "wwwroot\\images\\Actvot",
                            file);

                        using (var stream = new FileStream(path, FileMode.Create))
                        {
                            await view.ImageFile.CopyToAsync(stream);
                        }

                        path = $"~/images/Actvot/{file}";
                    }
                    var Actvote = this.ToActvote(view, path);
                    Actvote.user = await this.userHelper.GetUserByEmailAsync(this.User.Identity.Name);

                    await this.actVoteRepository.UpdateAsync(Actvote);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!await this.actVoteRepository.ExistAsync(view.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(view));
        }