I have the following code to populate a DropDownList
var list = new Dictionary<string, decimal>();
list.Add("1.000.000", 1000000m);
list.Add("500.000", 500000m);
list.Add("5.000", 5000m);
viewModel.MyValue = 500000.00m; //of type decimal?
viewModel.MyList = new SelectList(list, "Value", "Key", viewModel.MyValue);
While in the Html
@Html.DropDownListFor(model => model.MyValue, Model.MyList, "select value")
All works fine (the selected value is passed to the controller and saved regularly), the only thing not working is the selected value. The one I pass is not automatically selected in the dropdown. Am I missing something obvious?
I solved the problem.
The framework while doing the ToString() in the SelectList was unable to match 500000.00 with 500000 (I put the wrong value in my question, sorry for the typo. updated)
Using a simple froeach you can see what happen
So I solve the problem simply doing
Thanks all for the help anyway