public async Task <IActionResult> CreateEdit(int?id, PlayerCreateEditViewModel playerCreateEditModel) { try { if (ModelState.IsValid) { if (id.HasValue) { var result = await Edit(id.Value, playerCreateEditModel); return(result); } else { var result = await Create(playerCreateEditModel); return(result); } } return(View("CreateEdit", playerCreateEditModel)); } catch { return(View("Error")); } }
public async Task <IActionResult> CreateEdit(int?id) { var playerEditCreateModel = new PlayerCreateEditViewModel { Player = new PlayerModel() }; if (id.HasValue && id.Value != 0) { var client = new HttpClient(); var url = $"https://localhost:5001/player/{id}"; var responce = await client.GetAsync(url); if (responce.IsSuccessStatusCode) { var content = await responce.Content.ReadAsStringAsync(); var dto = JsonConvert.DeserializeObject <PlayerViewDto>(content); playerEditCreateModel.Player = _mapper.Map <PlayerModel>(dto); } } playerEditCreateModel.Teams = await GetTeams(); return(View("CreateEdit", playerEditCreateModel)); }
private async Task <IActionResult> Create(PlayerCreateEditViewModel playerCreateModel) { var playerToCreate = playerCreateModel.Player; if (playerCreateModel.IsNewTeam) { var newTeamName = playerCreateModel.NewTeamName; var newTeamId = await CreateTeam(newTeamName); playerToCreate.TeamId = newTeamId; } var client = new HttpClient(); var url = $"https://localhost:5001/player/"; var dto = _mapper.Map <PlayerCreateDto>(playerToCreate); var json = JsonConvert.SerializeObject(dto); var data = new StringContent(json, Encoding.UTF8, "application/json"); var responce = await client.PostAsync(url, data); if (responce.IsSuccessStatusCode) { return(RedirectToAction(nameof(Index))); } return(View("Error")); }
// GET: players/Create public ActionResult Create() { var vm = new PlayerCreateEditViewModel(); vm.MapSelectList = new SelectList(_uow.Maps.All, nameof(Map.MapId), nameof(Map.MapName)); vm.TeamSelectList = new SelectList(_uow.Teams.All, nameof(Team.TeamId), nameof(Team.TeamName)); // ViewBag.MapId = new SelectList(_uow.Maps.All, "MapId", "MapName"); // ViewBag.TeamId = new SelectList(_uow.Teams.All, "TeamId", "TeamName"); return(View(vm)); }
public ActionResult Edit(PlayerCreateEditViewModel vm) { if (ModelState.IsValid) { // do not get user id from html get/post!!!! vm.Player.UserId = User.Identity.GetUserId <int>(); _uow.Players.Update(vm.Player); _uow.Commit(); return(RedirectToAction(nameof(Index))); } vm.MapSelectList = new SelectList(_uow.Maps.All, nameof(Map.MapId), nameof(Map.MapName), vm.Player.MapId); vm.TeamSelectList = new SelectList(_uow.Teams.All, nameof(Team.TeamId), nameof(Team.TeamName), vm.Player.TeamId); return(View(vm)); }
// GET: players/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } // check user id!!!! Player player = _uow.Players.GetForUser(id.Value, User.Identity.GetUserId <int>()); if (player == null) { return(HttpNotFound()); } var vm = new PlayerCreateEditViewModel() { Player = player }; vm.MapSelectList = new SelectList(_uow.Maps.All, nameof(Map.MapId), nameof(Map.MapName), vm.Player.MapId); vm.TeamSelectList = new SelectList(_uow.Teams.All, nameof(Team.TeamId), nameof(Team.TeamName), vm.Player.TeamId); return(View(vm)); }