我在預選列表框中的項目時遇到問題。 我使用mvc 3的剃鬚刀視圖引擎。我知道有幾個帖子有同樣的問題,但他們不適合我。Multiselect-Listbox(MVC3 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"})
我試過很多其他的東西,但沒有奏效。希望有人能幫忙。
非常感謝。我得到了它的工作。雖然只有第二個代碼有效,但在我看來,這不是因爲重寫的ToString()。我的Id屬性是一個int,我添加了一個返回一個唯一字符串(guid.ToString())的Property Key。 – Tobias 2011-05-03 06:17:37
奇怪。無法解釋ToString的事情,但很高興你得到它排序:-) – 2011-05-03 07:24:15
由於ToString(),它是最初的。剛剛在微軟論壇上發佈了一個問題,並希望得到一個答案,也許信息爲什麼(以及多久)這是他們目前的方式。 – Tobias 2011-05-06 08:26:54