與其試圖將2個選擇列表連接在一起,爲什麼不把它們合併在一起?您還沒有提供太多的信息,但假設你使用LINQ
和假設你的城市和國家之間的關係分貝...
// get the city & state data
var cities = (from c in GetAllCities()
orderby c.Name
orderby c.State.Name
select new
{
cityid = c.Id,
cityname = c.Name,
state = e.State.Name
}).ToList();
// convert the data into a list of `SelectListItem`
var items = from cs in cities.AsEnumerable()
select new SelectListItem
{
Value = cs.cityid.ToString(),
Text = string.Format("{0} {1} ({2})", cs.cityname, cs.state)
};
// create the selectlist
return new SelectList(items , "Value", "Text");