【发布时间】:2011-08-17 01:14:31
【问题描述】:
我在列表框中预选项目时遇到问题。 我正在使用带有 mvc 3 的 razor 视图引擎。我知道有一些帖子存在相同的问题,但它们对我不起作用。
类中的代码:
public class Foo{
private int _id;
private string _name;
public string Name{
get{
return _name;
}
public int Id {
get{
return _id;
}
}
模型中的代码:
public class FooModel{
private readonly IList<Foo> _selectedFoos;
private readonly IList<Foo> _allFoos;
public IList<Foo> SelectedFoos{
get{ return _selectedFoos;}
}
public IList<Foo> AllFoos{
get{ return _allFoos;}
}
}
cshtml中的代码:
@Html.ListBoxFor(model => model.Flatschels,
Model.AllFlatschels.Select(fl => new SelectListItem {
Text = fl.Name,
Value = fl.Id.ToString(),
Selected = Model.Flatschels.Any(y => y.Id == fl.Id)
}), new {Multiple = "multiple"})
我尝试了很多其他方法,但都没有奏效。希望有人能帮忙。
【问题讨论】:
标签: .net asp.net-mvc asp.net-mvc-3 razor