I've read a bunch of questions about the subject but haven't managed to find a solution to this specific problem.
Controller
public ActionResult Index() {
string categorie = "--Tout--";
string souscategorie = "--Tout--";
if (Session["Categorie"] != null) {
categorie = Session["Categorie"].ToString();
}
if (Session["SousCategorie"] != null) {
souscategorie = Session["SousCategorie"].ToString();
}
SelectList cats = new SelectList(GetCategories(), categorie);
SelectList sCats = new SelectList(GetSousCategories(), souscategorie);
ViewBag.Categories = cats;
ViewBag.SousCategories = sCats;
using(DAL.WebShopEntities entities = new WebShopEntities()) {
return View(entities.Article.ToList());
}
}
View
@Html.Label("Catégories")
@Html.DropDownList("Categories", (SelectList)ViewBag.Categories, new { @class = "form-control dropdownlist" })
<br />
@Html.Label("Sous-Catégories")
@Html.DropDownList("SousCategories", (SelectList)ViewBag.SousCategories, new { @class = "form-control dropdownlist" })
When debugging the view I can see clearly that the option stocked in the session is sent to the View. But it displays the index 0 when checking in the browser. This has me bugging because the SelectList behaves normally, I think the problem lays with the DropDown but what could be the problem?