public async Task <IActionResult> Edit(int id, [Bind("Id,FruitCountPerPage,WeaponCountPerPage,PirateGroupCountPerPage,PersonCountPerPage")] Setting setting) { if (id != setting.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(setting); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SettingExists(setting.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(setting)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Description,ImagePath")] PirateGroup pirateGroup, string[] selectedPersons) { if (id != pirateGroup.Id) { return(NotFound()); } var pirateGroupToUpdate = await _context.PirateGroups.Include(g => g.Persons).SingleOrDefaultAsync(g => g.Id == id); // Check if the name exists already if (_context.PirateGroups.Any(group => group.Name == pirateGroup.Name && group.Id != pirateGroup.Id)) { ViewBag.NameExists = _localizer["Name '{0}' already exists.", pirateGroup.Name]; PopulateAssignedPerson(selectedPersons); return(View(pirateGroup)); } if (ModelState.IsValid) { // Update persons // 把当前的object,用controller创建来的object更新 await TryUpdateModelAsync <PirateGroup>(pirateGroupToUpdate, "", i => i.Name, i => i.Description, i => i.ImagePath); UpdatePirateGourpPersons(pirateGroupToUpdate, selectedPersons); // Try to upload file if (!(await TryUploadFile(pirateGroupToUpdate))) { PopulateAssignedPerson(pirateGroupToUpdate); return(View(pirateGroupToUpdate)); } // Update DB try { _context.Update(pirateGroupToUpdate); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PirateGroupExists(pirateGroup.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(pirateGroup)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Description,ImagePath")] Weapon weapon) { if (id != weapon.Id) { return(NotFound()); } if (_context.Weapons.Any(w => w.Name == weapon.Name && w.Id != weapon.Id)) { ViewBag.NameExists = _localizer["Name '{0}' already exists.", weapon.Name]; return(View(weapon)); } if (ModelState.IsValid) { // Try to upload file if (!(await TryUploadFile(weapon))) { return(View(weapon)); } // Update DB try { _context.Update(weapon); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!WeaponExists(weapon.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(weapon)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Type,Ability,Description,ImagePath")] Fruit fruit) { if (id != fruit.Id) { return(NotFound()); } // Check if name already exists if (_context.Fruits.Any(f => f.Name == fruit.Name && f.Id != fruit.Id)) { ViewBag.NameExists = _localizer["Name '{0}' already exists.", fruit.Name]; return(View(fruit)); } if (ModelState.IsValid) { // Try to upload file if (!(await TryUploadFile(fruit))) { return(View(fruit)); } // Update DB try { _context.Update(fruit); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FruitExists(fruit.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(fruit)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Nickname,Description,RewardMoney,Race,Sex,Birthday,FeatureType,Title,PirateGroupID,ImagePath")] Person person, string[] selectedFruits, string[] selectedWeapons) { if (id != person.Id) { return(NotFound()); } var personToUpdate = await _context.Persons .Include(p => p.FruitPossessions).ThenInclude(fp => fp.Fruit) .Include(p => p.WeaponPossessions).ThenInclude(wp => wp.Weapon) .SingleOrDefaultAsync(p => p.Id == id); // Check if the name exists already if (_context.Persons.Any(p => p.Name == person.Name && p.Id != person.Id)) { ViewBag.NameExists = _localizer["Name '{0}' already exists.", person.Name]; PopulatePirateGroupsDropDownList(person.PirateGroupID); PopulateAssignedFruit(selectedFruits); PopulateAssignedWeapon(selectedWeapons); return(View(person)); } if (ModelState.IsValid) { // Update fruits // 把当前的object,用controller创建来的object更新 await TryUpdateModelAsync <Person>(personToUpdate, "", i => i.Name, i => i.Nickname, i => i.Description, i => i.RewardMoney, i => i.Race, i => i.Sex, i => i.Birthday, i => i.FeatureType, i => i.Title, i => i.PirateGroupID, i => i.ImagePath); UpdatePersonFruits(personToUpdate, selectedFruits); UpdatePersonWeapons(personToUpdate, selectedWeapons); // Try to upload file if (!(await TryUploadFile(personToUpdate))) { PopulatePirateGroupsDropDownList(personToUpdate.PirateGroupID); PopulateAssignedFruit(personToUpdate); PopulateAssignedWeapon(personToUpdate); return(View(personToUpdate)); } // Reward money if (person.RewardMoney == null) { person.RewardMoney = 0; } // Update DB try { _context.Update(personToUpdate); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(person.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(person)); }