public async Task <IActionResult> Edit(int?id) // Este método serve para abrir o ecran de vendedor para o editar. { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Id não indicado" })); } var obj = await _sellerService.FindByIdAsync(id.Value); // obj recebe o _sellerService passando como argumento o id if (obj == null) { return(RedirectToAction(nameof(Error), new { message = "Id não encontrado" })); } List <Department> departments = await _departmentService.FindAllAsync(); // Se passou nos testes anteriores(Testes de não existe) leio os departamentos. SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; // Preencho o SellerFormViewModel com os dados de Seller do obj que acima fomos ler à Base de Dados. Preencho tambem os Departamentos. return(View(viewModel)); // retornar a View preenchida com os dados de viewModel. }
public IActionResult Edit(int?id) { if (id == null) { return(NotFound()); } //se o id nao for nulo, a tela de edit pode abrir var obj = _sellerService.FindById(id.Value); if (obj == null) { return(NotFound()); } //puxa os departamentos e popula a caixa de selecao List <Departament> departments = _departamentService.FindAll(); SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departaments = departments }; return(View(viewModel)); }
public async Task <IActionResult> Create(Seller seller) { // verifica se é valido if (!ModelState.IsValid) { var departments = await _departmentService.FindAllAsync(); var viewModel = new SellerFormViewModel { Seller = seller, Departments = departments }; return(View(viewModel)); } await _sellerService.InsertAsync(seller); // adiciona sales records SalesRecord sr = new SalesRecord(seller.Id, seller.BirthDate, seller.BaseSalary, SaleStatus.Billed, seller); await _salesRecordService.InsertAsync(sr); //redireciona a pagina return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Id not provided" })); } var obj = await _sellerService.FindByIdAsync(id.Value); if (obj == null) { return(RedirectToAction(nameof(Error), new { message = "Id not found" })); } List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Id não fornecido" }));; } Seller seller = await _sellerService.FindByIdAsync(id.Value); if (seller == null) { return(RedirectToAction(nameof(Error), new { message = "Id não encontrado" }));; } List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel { Seller = seller, Departments = departments }; return(View(viewModel)); }
public async Task <IActionResult> Create(Seller seller) { IActionResult result = null; if (!ModelState.IsValid) // validate if the JavaScript is enabled and already validated this record { var departmets = await _DepartmentService.FindAllAsync(); var viewModel = new SellerFormViewModel { Departments = departmets, Seller = seller }; result = View(viewModel); } else { await _SellerService.InsertAsync(seller); result = RedirectToAction(nameof(Index)); } return(result); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var obj = await _sellerService.FindByIdAsync(id.Value); if (obj == null) { return(NotFound()); } List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int id, Seller seller) { if (!ModelState.IsValid) { var departments = await _departmentService.FindAllAsync(); var viewModel = new SellerFormViewModel { Seller = seller, Departments = departments }; return(View(viewModel)); } if (id != seller.Id) { return(RedirectToAction(nameof(Error), new { Message = "Id mismatch" })); } try { await _sellerService.UpdateAsync(seller); return(RedirectToAction(nameof(Index))); } /* catch (ApplicationException e) => sintaxe opcional para não ficar usando varios catch, por ser superclasse das duas exeções, irá funcionar igual graças ao upcasting * { * return RedirectToAction(nameof(Error), new { Message = e.Message }); * } */ catch (NotFoundException e) { return(RedirectToAction(nameof(Error), new { Message = e.Message })); } catch (DbConcurrencyException e) { return(RedirectToAction(nameof(Error), new { Message = e.Message })); } }
public async Task <IActionResult> Edit(int?id) { if (id is null) { return(RedirectToAction(nameof(Error), new { message = "Id is null" })); } var seller = await _sellerService.FindAsync(id.Value); if (seller is null) { return(RedirectToAction(nameof(Error), new { message = "Id not found" })); } var departments = await _departmentService.FindAllAsync(); var sellerFormViewModel = new SellerFormViewModel { Seller = seller, Departments = departments }; return(View(sellerFormViewModel)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(BadRequest(new { message = "Id not provided" })); } var obj = await _sellerService.FindByIdAsync(id.Value); if (obj == null) { return(NotFound(new { message = "Id not found" })); } List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; return(Ok(viewModel)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { messagem = "Id não Informado" })); } var obj = await _serviceVendedor.FindByIdAsync(id.Value); if (obj == null) { return(RedirectToAction(nameof(Error), new { messagem = "Id não Existe" })); } List <Departamento> departamentos = await _serviceDepartamento.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel { Vendedor = obj, Departamentos = departamentos }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int?id) // acho q quando tem o ? eh a versao get do método { if (id == null) // se o id for nulo quer dizer que a requisição foi feita de uma forma indevida { return(RedirectToAction(nameof(Error), new { message = "Id not provided" })); } var obj = await _sellerService.FindByIdAsync(id.Value); if (obj == null) { return(RedirectToAction(nameof(Error), new { message = "Id not found" })); } List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; // instanciação do viewmodel return(View(viewModel)); }
// GET action to the link "Edit" created at Sellers page that will call View Edit Form public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Id not provided" })); } var obj = await _sellerService.FindByIdAsync(id.Value); // syntax: FindById(id.value) - Because in the beginning of the method the passages of ID was set as optionally. if (obj == null) { return(RedirectToAction(nameof(Error), new { message = "Id not found" })); } List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "ID NOT FOUND" })); } var seller = await _sellerService.FindByIdAsync(id.Value); if (seller == null) { return(RedirectToAction(nameof(Error), new { message = "SELLER NOT FOUND" })); } var departments = await _departmentService.FindAllAsync(); var viewModel = new SellerFormViewModel { Departments = departments, Seller = seller }; return(View(viewModel)); }
// GET: Sellers/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Id not provided" })); } Seller seller = await _sellerService.FindByIdAsync(id ?? default(int)); if (seller == null) { return(RedirectToAction(nameof(Error), new { message = "Seller not found" })); } var departments = await _departmentService.FindAllAsync(); var viewModel = new SellerFormViewModel { Departments = departments, Seller = seller }; return(View(viewModel)); }
//função de editar GET public async Task <IActionResult> Edit(int?id) {//testar se o id é null if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Id não fornecido." })); } //testar se ele existe no db var obj = await _sellerService.FindByIdAsync(id.Value); if (obj == null) { return(RedirectToAction(nameof(Error), new { message = "Id não existe!" })); } //abrir a tela de edição carregando os departamentos e povoando a caixa de edição List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int?Id) { if (Id == null) { return(RedirectToAction(nameof(Error), new { message = "Id not Provided!" })); } var result = await _sellerService.FinByIdAsync(Id.Value); if (result == null) { return(RedirectToAction(nameof(Error), new { message = "Id not Found!" })); } List <Department> departments = await _departmentService.FindAllAsync(); var viewModel = new SellerFormViewModel { Departments = departments, Seller = result }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int?id) { if (string.IsNullOrEmpty(id.ToString()) || string.IsNullOrWhiteSpace(id.ToString())) { return(RedirectToAction(nameof(Error), new { message = "Id não foi fornecido!" })); } var seller = await _sellerService.FindByIdAsync(id.Value); if (seller == null) { return(RedirectToAction(nameof(Error), new { message = "Id não encontrado!" })); } List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel { Seller = seller, Departments = departments }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int?id) // ? = Parametro opcional para não dar erro na entrada { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Id not provided" })); } var obj = await _sellerService.FindByIdAsync(id.Value); // Como o parametro é opcional deve-se o usar o value if (obj.Equals(null)) { return(RedirectToAction(nameof(Error), new { message = "Id not found" })); } List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) //existe o id é igual a nulo? ele foi informado? { return(RedirectToAction(nameof(Error), new { message = "Id not provided" })); } var obj = await _sellerService.FindByIdAsync(id.Value); if (obj == null) //existe no DB este vendedor deste id? { return(RedirectToAction(nameof(Error), new { message = "Id not found" })); } List <Department> departments = await _departmentService.FindAllAsync(); //carregar os departamentos para povoar a caixa de seleção da view SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; //como será uma tela de edição, preencheremos ja os campos dados do obj. Passaremos o departments também. return(View(viewModel)); }
public async Task <IActionResult> Edit(int id, Seller seller) { if (!ModelState.IsValid) { var departments = await _departmentService.FindAllAsync(); var viewModel = new SellerFormViewModel { Seller = seller, Departments = departments }; return(View(viewModel)); } if (id != seller.Id) { return(RedirectToAction(nameof(Error), new { message = "Id mismatch" })); } try { await _sellerService.UpdateAsync(seller); return(RedirectToAction(nameof(Index))); } catch (ApplicationException e) { return(RedirectToAction(nameof(Error), new { message = e.Message })); } /* ApplicationException ja faz o servico dos dois catchs abaixo. (upcasting) * catch (NotFoundException e) * { * return RedirectToAction(nameof(Error), new { message = e.Message }); * } * catch (DbConcurrencyException e) * { * return RedirectToAction(nameof(Error), new { message = e.Message }); * } */ }
public IActionResult Edit(int id, Seller seller) { if (!ModelState.IsValid) //!ModelState.IsValid = se p modelstate não foi validado, ele entra e quebra o método { var departments = _departmentService.FindAll(); var viewModel = new SellerFormViewModel { Seller = seller, Departments = departments }; return(View(viewModel)); }//para evitar que a pessoa sem javascript envie um cadastro vazio if (id != seller.Id) //confirma se o id que estamos tentando editar é o mesmo que está sendo mostrado { return(RedirectToAction(nameof(Error), new { message = "Id mismatch" }));//tratamento do erro, ids não correspondem } try { _sellerService.Update(seller); return(RedirectToAction(nameof(Index))); } catch (ApplicationException e) {//ApplicationException tratamento geral de erros, se o id não existe ou não é o mesmo return(RedirectToAction(nameof(Error), new { message = e.Message })); } }
public async Task <IActionResult> Edit(int id, Seller seller) { // ser receber um seller vazio ou inválido o lado do servidor irá retornar para a página if (!ModelState.IsValid) { var departments = await _departmentService.FindAllAsync(); var viewModel = new SellerFormViewModel { Seller = seller, Departments = departments }; return(View(viewModel)); } if (id != seller.Id) { return(RedirectToAction(nameof(Error), new { message = "Id mismatch" })); } try { await _sellerService.UpdateAsync(seller); return(RedirectToAction(nameof(Index))); } catch (ApplicationException e) // super tipo das duas exceções { return(RedirectToAction(nameof(Error), new { message = e.Message })); } /*catch (NotFoundException e) * { * * return RedirectToAction(nameof(Error), new { message = e.Message }); * } * catch (DbConcurrencyException e) * { * RedirectToAction(nameof(Error), new { message = e.Message}); * } */ }
public async Task <IActionResult> Edit(int Id, Seller seller) { if (!ModelState.IsValid) { var depatments = await _departmentService.FindAllAsync(); var viewModel = new SellerFormViewModel { Seller = seller, Departments = depatments }; return(View(viewModel)); } if (Id != seller.Id) { return(RedirectToAction(nameof(Error), new { message = "Id mismatch" })); } try { await _sellerService.UpdateAsync(seller); return(RedirectToAction(nameof(Index))); } catch (NotFoundException e) { return(RedirectToAction(nameof(Error), new { message = e.Message })); } catch (DbConcurrencyException e) { return(RedirectToAction(nameof(Error), new { message = e.Message })); } // Upcasting subistituindo os dois catchs por este. //catch (ApplicationException e) //{ // return RedirectToAction(nameof(Error), new { message = e.Message }); //} }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Identifier Not Found!" })); } var obj = await _sellerService.FindByIdAsync(id.Value); if (obj is null) { return(RedirectToAction(nameof(Error), new { message = "Object is null." })); } List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel sellerFormViewModel = new SellerFormViewModel() { Seller = obj, Departments = departments }; return(View(sellerFormViewModel)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { errorMessage = "Id not Providade" })); } var seller = await _sellerService.FindByIdAsync(id.Value); if (seller == null) { return(RedirectToAction(nameof(Error), new { errorMessage = "Id not Found" })); } List <Department> departments = await _departmentService.FindAllAsync(); SellerFormViewModel viewModel = new SellerFormViewModel() { Departments = departments, Seller = seller }; return(View(viewModel)); }
public async Task <IActionResult> Create(Seller seller) { try { if (!ModelState.IsValid) { var departments = await _departmentService.FindAll(); var viewModel = new SellerFormViewModel(seller, departments); return(View(viewModel)); } await _sellerService.CreateSeller(seller); return(RedirectToAction(nameof(Index))); } catch { return(RedirectToAction(nameof(Error), new { message = "Ops... Ocorreu um erro!" })); } }
//Método GET public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Id not provided" })); } var obj = await _sellerService.FindByiDAsync(id.Value); if (obj == null) { return(RedirectToAction(nameof(Error), new { message = "Id not found" })); } List <Department> departments = await _departmentService.FindAllAsync(); //Busca a lista de departamentos no DB //Chama a tela de edição com os campos já preenchidos, instanciando a ViewModel com o Depatamento do Obj encontrado; SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; return(View(viewModel)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Sorry Seller Id not provided." })); } var obj = await _sellerservice.FindbyIdAsync(id.Value); if (obj == null) { return(RedirectToAction(nameof(Error), new { message = "Sorry Seller Id not found." })); } // carrega a tela de edicao carregando dados List <Department> departments = await _departmentservice.FindAllAsync(); SellerFormViewModel viewmodelseller = new SellerFormViewModel { Seller = obj, Departments = departments }; return(View(viewmodelseller)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction(nameof(Error), new { message = "Id not Provided" })); } var obj = await _sellerService.FindByIdAsync(id.Value); if (obj == null) { return(RedirectToAction(nameof(Error), new { message = "Id not Found" })); } // para jogar na tela de edição, busco todos os departamentos para alimentar o dropdown List <Department> departments = await _departmentService.FindAllAsync(); //agora instancio a viewModel de Seller passando o obj encontrado a partir do id e a lista de departamentos SellerFormViewModel viewModel = new SellerFormViewModel { Seller = obj, Departments = departments }; return(View(viewModel)); }