我在控制器中创建了一个selectList,以便在视图中显示。
我试图在飞行中创建它,某种程度上是这样的..像这样..。
myViewData.PageOptionsDropDown =
new SelectList(new [] {"10", "15", "25", "50", "100", "1000"}, "15");
它可以编译,但输出不好...
<select id="PageOptionsDropDown" name="PageOptionsDropDown">
<option>10</option>
<option>15</option>
<option>25</option>
<option>50</option>
<option>100</option>
<option>1000</option>
</select>
注意到没有选择任何项目了吗?
我该怎么解决这个问题??
发布于 2009-04-23 14:33:06
我就是这么做的
IList<Customer> customers = repository.GetAll<Customer>();
IEnumerable<SelectListItem> selectList =
from c in customers
select new SelectListItem
{
Selected = (c.CustomerID == invoice.CustomerID),
Text = c.Name,
Value = c.CustomerID.ToString()
};
再看一眼我不确定我知道你在找什么。
发布于 2009-05-18 13:49:13
我使用了一个扩展方法:
用法
var departmentItems = departments.ToSelectList(d => d.Code +
" - " + d.Description,
d => d.Id.ToString(),
" - ");
var functionItems = customerFunctions.ToSelectList(f => f.Description,
f => f.Id.ToString(),
" - ");
使用
public static class MCVExtentions
{
public static List<SelectListItem> ToSelectList<T>(
this IEnumerable<T> enumerable,
Func<T, string> text,
Func<T, string> value,
string defaultOption)
{
var items = enumerable.Select(f => new SelectListItem()
{
Text = text(f),
Value = value(f)
}).ToList();
items.Insert(0, new SelectListItem()
{
Text = defaultOption,
Value = "-1"
});
return items;
}
}
发布于 2009-04-23 14:35:35
使用接受items, dataValueField, dataTextField, selectedValue
作为参数的构造函数:
ViewData["myList"] =
new SelectList(new[] { "10", "15", "25", "50", "100", "1000" }
.Select(x => new {value = x, text = x}),
"value", "text", "15");
然后在您的视图中:
<%=Html.DropDownList("myList") %>
https://stackoverflow.com/questions/781987
复制相似问题