Return View without losing data

Asked

Viewed 288 times

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);
    }

1 answer

2


You need to return the same as your LSE, passing the viewmodel back to view:

return View(ambienteViewModel);

So the viewmodel will continue to be filled and the fields will be reloaded. Now if you want to make an asynchronous call, you can use AJAX and JQUERY for this, Razor supports it, but you need to look for documentation regarding the version of MVC you are using.

Follows a reference in MVC 5: https://www.c-sharpcorner.com/article/asp-net-mvc5-razor-ajax-form-control/

  • 1

    Wow, the answer was in my face kk, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.