コード例 #1
0
        public async Task <IActionResult> Update(Guid id, CreateOrUpdateApplicationViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (!model.Id.HasValue || model.Id.Value != id)
                {
                    throw new ArgumentException("Invalid Id in model");
                }

                var modelWithSameName = await _store.FindByName(model.Name);

                if (modelWithSameName == null || modelWithSameName.Id == id)
                {
                    await _store.Update(id, x =>
                    {
                        x.Name = model.Name;
                    });

                    return(RedirectToAction(nameof(List)));
                }

                ModelState.AddModelError("Name", "Name already in use");
            }

            // If we got this far, something failed, redisplay form
            return(View("CreateOrUpdate", model));
        }
コード例 #2
0
        public async Task <IActionResult> Create(CreateOrUpdateApplicationViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (await _store.FindByName(model.Name) == null)
                {
                    await _store.Create(new Domain.Entities.Application
                    {
                        Name = model.Name
                    });

                    return(RedirectToAction(nameof(List)));
                }

                ModelState.AddModelError("Name", "Name already in use");
            }

            // If we got this far, something failed, redisplay form
            return(View("CreateOrUpdate", model));
        }