3
I have a code in my controller
that makes some procedures, if any of these procedures go wrong, it returns me to view
that I desire and in the view I display some messages, this mine view
is a view
form.
What’s going on:
When I finish the method in the controller I use return View();
, but it returns me the empty fields, it reloads the whole page, I would like it to return to view
with the data that were already filled, I thought to use Return RedirectToAction("Create");
but the same thing happens, would have some kind of return(it is necessary to have a return in my method that did not reload the page?
My code in the controller:
[HttpPost]
public async Task<IActionResult> Create(TipoPecaViewModel ambienteViewModel)
{
if (ModelState.IsValid)
{
TipoPecaModel ambienteModel = new TipoPecaModel();
ambienteModel.TpPeca_Codigo = ambienteViewModel.TpPeca_Codigo;
ambienteModel.GrupoProduto_Codigo = ambienteViewModel.GrupoProduto_Codigo;
ambienteModel.TpPeca_Sigla = ambienteViewModel.TpPeca_Sigla;
ambienteModel.TpPeca_Situacao = ambienteViewModel.TpPeca_Situacao.Equals(true) ? "A" : "D";
ambienteModel.usr_cod_criacao = Convert.ToInt16(Services.Token.strCod_Usuario);
ambienteModel.usr_dt_hr_criacao = DateTime.Now.Date;
ambienteModel.Emp_codigo = Services.Token.strEmp_codigo;
var UrlApi = "api/TipoPeca/create";
Uri BaseAdress = Services.Token.BaseAdress;
string strToken = Services.Token.strToken;
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = BaseAdress;
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", strToken);
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("Application/Json"));
var stringContent = new StringContent(JsonConvert.SerializeObject(ambienteModel), Encoding.UTF8, "application/json");
using (HttpResponseMessage response = await httpClient.PostAsync(UrlApi, stringContent))
{
var teste = (int)response.StatusCode;
if(teste == 400){
validar2 = "True";
return View();
}else if(teste == 409){
validar2 = "Trues";
return View();
}
response.EnsureSuccessStatusCode();
return Redirect("/TipoPeca/Fechar");
}
}
}
else
return View(ambienteViewModel);
}
Wow, the answer was in my face kk, thank you
– Jeff Henrique