I have a problem with mvc DropDownList, lots of topics about that, but not one with the same problem.
I want a default selected option for my DropDownList, but i also need additional option for "all" items selection.
So my controller is binding default value of 2 to dropdown
public ActionResult Index(int? All = 2){ ...
In cshtml i have
@Html.DropDownList("All","All items")
All list is filled like this
ViewData["All"] = new SelectList(CommonLists.property_types.Select(x => new { v = x.Value, t = x.Key.ToLower() }), "v", "t", All);
property_types
public static Dictionary<string, int> property_types = new Dictionary<string, int>() {
{ "House", 1 }, { "Flat", 2 }, { "Garden", 3 }
So it should work like this
- when first entered the default selected value is 2, equals to "flat", only "flat" items are displayed (works)
- user can change it to other option from the list (works)
- user can change to view all items at once no matter the category selecting "All items" (this is not working)
I have assumed it should work, but to my surprise, when i select "All items" mvc does not return null it just returns the default int value 2, so basically no way to query for all items.
Is this suppose to work like that? The auto generated "All items" is empty value so i assumed mvc would translate it to null, but it is not.
How to fix this?