asp.net-mvc persist state views to return to previous states

0

I write the post because I have been thinking about how to solve a problem that I have to solve and I do not know how to approach it ... (I feel the brick, it's very conceptual all ...).

The fact is that I'm with an application that has 5 main views, two of them "informative" and the other three linked in terms of operation, I explain:

In the first view, the search criteria are selected, search is clicked and the selection of the combos and others is sent to the controller, who performs the search and launches the list of results with an extract of the info to the second view. In that second view you select in which you are interested and send you to the third view with the details of the selected ones.

Depending on the operation of the application, at any time you may be interested in going to one of the explanatory views and then return to one of the functional views, or in any of steps two or three, go back to step one or two (from the results back to the search or from the detail back to the search or the results) to refine the search criteria or to select a different set of results to show in detail.

Therefore I am in the history of seeing how I can persist the conditions of the functional views of the search and the list to be able to return to them with the last selected criteria, from there to be able to select new ones.

After thinking about it a lot has occurred to me (although academically it is a brunette, I know) when you go through the controller method and generate the model for the view, save it in a TempData and to return to the other buttons that call to other controller methods that check if there is a stored model and if they are loaded, and if they do not load that view from scratch, the code remains as follows:

public ActionResult Busqueda()  
{
    BusquedaViewModel modelo = new BusquedaViewModel();

    // generamos modelo para la busqueda
    ... modelo ...

    // almacenamos el viewmodel para cuando vuelvan con los botones esté persistido
    TempData["busqueda"] = modelo;
    TempData.Keep("busqueda"); 

    return View("Busqueda", modelo);
}


public ActionResult Resultados(BusquedaViewModel busqueda)  
{
    // almacenamos el viewmodel para cuando vuelvan con los botones esté persistido
    TempData["busqueda2"] = busqueda;
    TempData.Keep("busqueda2"); 

    // creamos el modelo y realizamos la busqueda
    ResViewModel elModelo = new ResViewModel();

    // validamos el modelo
    if (ModelState.IsValid)
    {
        // el título de la página
        ... generamos elModelo del detalle de los resultados...


        // almacenamos el viewmodel para cuando vuelvan con los botones de herramientas esté persistido
        TempData["resultados"] = elModelo;
        TempData.Keep("resultados"); 


        // mostramos la vista
        return View("Resultados", elModelo);
    }
    else
    {
        return this.Busqueda(); 
    }
}


public ActionResult LosResultados(string store)
{   
    if (store.Equals("anterior", StringComparison.InvariantCultureIgnoreCase) && TempData["resultados"] != null)
    {
        ResViewModel modelo = (ResViewModel)TempData["resultados"];
        return View("Resultados", modelo);
    }
    else
    {
        return this.Busqueda();
    }
}       


public ActionResult LaBusqueda(string store)
{   
   if (store.Equals("yes", StringComparison.InvariantCultureIgnoreCase) && TempData["busqueda"] != null)  // TempData["busqueda2"]
   {
       BusquedaViewModel modelo = (BusquedaViewModel)TempData["busqueda"];
       return View("Busqueda", modelo);
   }
   else
   {
       return this.Busqueda();
   }
}

and the buttons of this style in the view:

<a href="@Url.Action("LaBusqueda", "App", new { store = "yes" })" class="" onclick="" title='Buscar' >
    <i class='@Literales.IClassBusqueda'></i><span class="btn">@Literales.Busqueda</span>
</a>

<a href="@Url.Action("LosResultados", "App", new { store = "yes" })" class="" onclick="" title='Resultados' >
    <i class='@Literales.IClassResultados'></i><span class="btn">@Literales.Resultados</span>
</a>

And the questions are these:

Do you think you can do this better? (Session ?? localStorage ?? others ??)

For the case of the results (which is a list of elements of a class) it works fine, but in the case of the search no. If I use the TempData "search2" it only has the selections of the combos and so on, but not the element lists of the combos. If I use the TempData "search" it does not have the selections and there are also some lists that tell me that you can only use them once because they come from a query (they are "searched" with the execution of a stored procedure) and as they were previously used Casca ... How could I solve this? Thank you very much in advance.

Greetings, Hector.

    
asked by sanmolhec 13.04.2018 в 09:39
source

0 answers