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

2

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

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

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

What can I do with it?

    
asked by anonymous 15.10.2014 / 14:58

1 answer

2

As you click 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 Details link to pass filter values to View that displays the query result:

@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 Details, you can reload the previous screen (containing the results of the query) when the user clicks "back" their detail view.

Example on Controller of how to receive values from the filters:

[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 Details view you have the values of the filters and you can pass them as a parameter on your "Back" link to reassemble your View result query:

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

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

Example Controller of the query receiving values from the filters:

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