public async Task <IActionResult> Edit([FromForm] ViewModels.Users.EditViewModel model)
        {
            try
            {
                model.Errors.Items.Clear();

                if (model == null)
                {
                    model.Errors.Items.Add("Пустой параметр");
                    return(PartialView(model));
                }
                if (string.IsNullOrEmpty(model.Username))
                {
                    model.Errors.Items.Add("Не указано имя пользователя.");
                    return(PartialView(model));
                }

                var entity = await context.Users.FindAsync(model.Id);

                entity.Id          = model.Id;
                entity.Email       = model.EMail;
                entity.UserName    = model.Username;
                entity.PhoneNumber = model.Phone;

                context.Update(entity);
                await context.SaveChangesAsync();


                //var result = await userManager.UpdateAsync(new Models.User
                //{
                //	Id = model.Id,
                //	Email = model.EMail,
                //	UserName = model.Username,
                //	PhoneNumber = model.Phone
                //});

                //if (!result.Succeeded)
                //{
                //	foreach (var er in result.Errors)
                //	{
                //		if (er.Code == "DuplicateUserName")
                //			model.Errors.Items.Add($"Пользователь \'{model.Username}\' уже существует");
                //	}
                //	return PartialView(model);
                //}
            }
            catch (Exception ex)
            {
                model.Errors.Items.Add(ex.Message);
                return(PartialView(model));
            }

            return(RedirectToAction("List"));
        }
Example #2
0
        public async Task <ActionResult> Edit(ViewModels.EditObjectViewModel viewModel)
        {
            try
            {
                await this.ShowUser(userManager);

                var model = new Models.Object
                {
                    Id      = viewModel.Id,
                    Address = viewModel.Address,
                    Name    = viewModel.Name
                };
                context.Entry(model);
                context.Update(model);
                await context.SaveChangesAsync();
            }
            catch
            {
            }
            return(View(viewModel));
        }