0

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?

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • You cannot use the same name for the property your binding to and `SelectList`. But your GET method is returning a collection, so your model could not even contain a property named `Categories` or `SousCategories` to bind to, so you view is not really making sense. –  Jan 16 '17 at 11:36
  • Hi Stephen, thanks for answering. Second statement is not entirely correct. I'm using a model indeed but I retrieve the data from the session to populate the dropdown so it works fine. What did you mean with your first statement please? – Yassin Hajaj Jan 16 '17 at 11:39
  • `List` does not contain any properties named `Categories` or `SousCategories` so despite what you may think, you view does not make sense. What is you model? Also suggest you read the answers [here](http://stackoverflow.com/questions/37161202/will-there-be-any-conflict-if-i-specify-the-viewbag-name-to-be-equal-to-the-mode/37162557#37162557) and [here](http://stackoverflow.com/questions/37407811/mvc5-razor-html-dropdownlistfor-set-selected-when-value-is-in-array/37411482#37411482) –  Jan 16 '17 at 11:41
  • Your view is actually quite confusing. Interpreting your view for the DropDownList will produce something like @Html.DropDownList("Categories", (SelectList)new SelectList(GetCategories(), categorie), new { @class = "form-control dropdownlist" }) where the GetCategories() has no meaning in the view – Alf Moh Jan 16 '17 at 12:23
  • @StephenMuecke Should've added that this is just a part of the view, not hte whole view. My model is used beneath the code I presented. Good read there thanks. – Yassin Hajaj Jan 16 '17 at 13:12
  • @AlfMoh Hey Alf, no it works fine, really believe me. :) – Yassin Hajaj Jan 16 '17 at 13:18
  • Are you sure about marking "selected" property correctly. Please double check if one of your select list item has "selected=true" – K D Jan 16 '17 at 13:44
  • @KD Yes I'm sure, while debugging and looking at the selectedvalue, even in the view, it is the value I expect, but Html.DropDown... doesn't display it. I changed it now to a html + razor's foreach loop but I find it less prettier of course :) – Yassin Hajaj Jan 16 '17 at 14:42
  • @YassinHajaj, Yes, I am aware of that :) Use a view model with properties `int SelectedCategory`, `IEnumerable CategoryList` (ditto for sub category) and a property for your collection and use `@Html.DropDownListFor(m => m.SelectedCategory, Model.CategoryList)` to strongly bind to your view model properties –  Jan 16 '17 at 21:35

1 Answers1

0

Can you try to change your Viewbag property name and try again?

try Categories => CategoriesList and SousCategories=> SousCategoriesList

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.CategoriesList = cats;
    ViewBag.SousCategoriesList = sCats;

    using(DAL.WebShopEntities entities = new WebShopEntities()) {
        return View(entities.Article.ToList());
    }
}



@Html.Label("Catégories")
@Html.DropDownList("Categories", (SelectList)ViewBag.CategoriesList, new { @class = "form-control dropdownlist" })

<br />

@Html.Label("Sous-Catégories")
@Html.DropDownList("SousCategories", (SelectList)ViewBag.SousCategoriesList, new { @class = "form-control dropdownlist" })
K D
  • 5,889
  • 1
  • 23
  • 35