I am trying to create a Dropdownlist for a list of the following class:
public class PrimulaPositionViewModel
{
public long Id { get; set; }
public Dictionary<string, string> Name { get; set; }
}
and in my cshtml view i call the dropdownlist like this:
@Html.DropDownList("Position", new SelectList(Model.Positions, "Id", "Name['sv']",
guise.PrimulaPositionId))
My issue is that "Name['sv']" doesn't get the value held in the dictionary and instead makes the program throw an error. If i were to just write Name then i would get an error but the dropdownlist simply outputs system.gen...... to represent the dictionary.
so i am wondering how i can access the Name["sv"] value when i am creating the SelectList in the view.
We usually put propertyName as dataTextField in SelectListItem.
If you want to use dictionary,you can use <select></select>
,and put Id and Name['sv'] into option in foreach:
<select>
@foreach (var item in Model.Positions)
{
if (item.Id == guise.PrimulaPositionId)
{
<option id="@item.Id" selected>@item.Name["sv"]</option>
}
else
{
<option id="@item.Id">@item.Name["sv"]</option>
}
}
</select>