Пример #1
0
        public async Task <IActionResult> Edit(Guid id,
                                               [Bind("Id,Title,Description,DOEnd,DOStart,CoverImage," +
                                                     "XuanxiangA," +
                                                     "XuanxiangB," +
                                                     "XuanxiangC," +
                                                     "XuanxiangD")]
                                               ZVote zVote)
        {
            if (id != zVote.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (zVote.CoverImage != null)
                    {
                        zVote.CoverPath = await ControllerMix.SaveFormFileAsync(
                            zVote.CoverImage, _env, ModelState,
                            nameof(zVote.CoverImage));
                    }
                    var _user = await _userManager.GetUserAsync(User);

                    zVote.SubmitterId = _user.Id;
                    zVote.Submitter   = _user;

                    _context.Update(zVote);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ZVoteExists(zVote.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(zVote));
        }
Пример #2
0
        public async Task <IActionResult> Create(
            [Bind("Id,Title,Description,DOEnd,DOStart,CoverImage," +
                  "XuanxiangA," +
                  "XuanxiangB," +
                  "XuanxiangC," +
                  "XuanxiangD")]
            ZVote zVote
            )
        {
            if (ModelState.IsValid)
            {
                if (zVote.Title == null || zVote.Title?.Length < 1)
                {
                    ModelState.AddModelError(
                        nameof(zVote.Title),
                        "请添加一个标题!!");
                    return(View(zVote));
                }
                if (zVote.Description == null || zVote.Description?.Length < 1)
                {
                    ModelState.AddModelError(
                        nameof(zVote.Description),
                        "请添加一个描述!!");
                    return(View(zVote));
                }
                if (zVote.XuanxiangA == null || zVote.XuanxiangA?.Length < 1)
                {
                    ModelState.AddModelError(
                        nameof(zVote.XuanxiangA),
                        "选项A和选项B要填写!");
                    return(View(zVote));
                }
                if (zVote.XuanxiangB == null || zVote.XuanxiangB?.Length < 1)
                {
                    ModelState.AddModelError(
                        nameof(zVote.XuanxiangB),
                        "选项A和选项B要填写!");
                    return(View(zVote));
                }

                zVote.Submitter = await _userManager.GetUserAsync(User);

                zVote.DOCreating = DateTimeOffset.Now;
                zVote.Id         = Guid.NewGuid();

                if (zVote.CoverImage != null)
                {
                    zVote.CoverPath = await ControllerMix.SaveFormFileAsync(
                        zVote.CoverImage, _env, ModelState,
                        nameof(zVote.CoverImage));
                }

                _context.Add(zVote);
                await _context.SaveChangesAsync();

                ViewData["IsEdit"] = false;
                return(RedirectToAction(nameof(Index)));
            }

            return(View(zVote));
        }