I want to pass a nested list (List<OptionValueList>
) to a View; then render each corresponding List<OptionValue>
inside @Html.DropdownListFor.
Class:
public class OptionValue
{
public int OptionID { get; set; }
public int ValueID { get; set; }
public string ValueName { get; set; }
}
public class OptionValueListList : List<OptionValueList> { }
public class OptionValueList : List<OptionValue> { }
I fill the class OptionValueListList
from db, but I couldn't adapt it in SelectListItem
in order to bind data to @HtmlDropdownListFor.
Below is my View:
@model Program.Models.OptionValueListList
@foreach (var item in Model) /*List<OptionValueList>*/
{
foreach (var y in item) /*List<OptionValue>*/
{
@Html.DropDownListFor(m=>y.ValueID, new SelectList(@ViewBag.x, "Value", "Text"), "Option Values")
}
}
And below is where I try to pass data and could not bind properly as it renders same dropdownlistfor for 18 times:
OptionValueListList o = new OptionValueListList();
o = pr.GetOptionsForProduct(id);
foreach (var item in o)
{
foreach (var i in item)
{
List<SelectListItem> li = new List<SelectListItem>();
li = (from c in item
select new SelectListItem
{
Text = c.ValueName,
Value = c.ValueID.ToString()
}).ToList();
ViewBag.x = li;
}
}
The problem in ViewBag.x = li;
Where should I put this Viewbag? Thanks.