How can I return to a Partialview, leaving the data loaded?

Asked

Viewed 372 times

2

I have an MVC system, where the user can make a query of the registrations. For this to happen, I have a View with filters and a PartialView that is updated within the View, displaying the results.

In front of each registration displayed, there is an option to see the details of it, where redirects the user to another View.

The problem is that when the user is in the Detail View, he can click "back", where he should return to the query page, with the results of the search performed. However, because it is a PartialView where the results are consulted I have to return to it. But this causes an Undefined Javascript error, as a Partial inside a View that has not been reloaded.

What can I do with it?

1 answer

2

As when clicking on Details the user is redirected to another View, uma alternativa é você passar os valores informados no(s) filtro(s) da consulta quando o usuário clicar na opção de ver os detalhes do cadastro.

Example of a link Details to pass filter values on View that displays the result of the query:

@Html.ActionLink("Detalhe", "Detalhes", "SeuController", new 
{
    id = model.Id, 
    filtro1 = model.filtro1, 
    filtro2 = model.filtro2
})

Receiving these values entered in the query filter(s) when the user clicks on Details, you can reload the previous screen (containing the results of the query) when the user clicks on "back" your Details View.

Example in Controller how to receive filter values:

[HttpPost]
public ActionResult Detalhes(int id, string filtro1, int filtro2)
{
    var model = new DetalhesViewModel(id, filtro1, filtro2); 
    // Carrega o model/sua partial com os dados dos filtros informados
    ...
}

Now, in your View of Details you have the values of the filters and you can pass them as parameter in your link "Back" to reassemble your View as a result of the consultation:

Example View: Passing the values of the filters in the link Back to reassemble the query screen

@Html.ActionLink("Voltar", "Consulta", "SeuController", new 
{
    filtro1 = model.filtro1, 
    filtro2 = model.filtro2
})

Example Controller of the query receiving filter values:

[HttpGet]
public ActionResult Consulta(string filtro1, int filtro2)
{
    // Carrega o model aproveitando os filtros
    var model = new ConsultaViewModel(filtro1, filtro2); 
    return View(model);
}

Browser other questions tagged

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