How do I relocate the selected item from a Category List to the first position

Asked

Viewed 34 times

0

In my application I have an area with several links of categories that is a Partialview.

The categories below comes from the following statement of Categoriascontroller

var categorias = db.Categorias.ToList();

Shoes | Health and Beauty | Supermarkets | Restaurants

To Actionresult of Partialview coded below gets to know which link the user clicked, through a Tempdata("filtroSelected").

[AllowAnonymous]
public ActionResult _CatalogoFiltroCategoria()
{
    TempData.Keep("filtroSelecionado");

    var categorias = db.Categorias.ToList();

    return PartialView(@"~/Views/Anuncios/_CatalogoFiltroCategoria.cshtml", categorias);
}

How do I make var categories set the selected link as the first of Tolist().

Click on Restaurants return me to the list like this.

Restaurants | Footwear | Health and Beauty | Supermarkets

Click on Health and Beauty return me to the list with Health and Beauty at the top of the list

Health and Beauty | Shoes | Supermarkets | Restaurants

1 answer

0

Using linq Voce can use the command insert passing the index and the item. Example:

Given your list Health and Beauty | Footwear | Supermarkets | Restaurants Imagine you want to put shoes on first, you’d do the following:

categorias.RemoveAt(1);
categorias.Insert(0,"Calçados");

what would make her so :

Shoes | Health and Beauty | Supermarkets | Restaurants

  • I confess that I did not understand your answer, I would give to exemplify better ?

  • edited in more detail

Browser other questions tagged

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