Eye*_*dic 24 asp.net-mvc html-helper asp.net-mvc-2 drop-down-menu
在我看来
<%= Html.DropDownListFor( x => x.Countries[ i ], Model.CountryList )%>
Run Code Online (Sandbox Code Playgroud)
在我的控制器中
public int[ ] Countries { get; set; }
public List<SelectListItem> CountryList { get; set; }
Run Code Online (Sandbox Code Playgroud)
当表单发布时没有问题,填充下拉列表并发布用户选择的值.但是当我尝试将已经指定值的表单加载到Countries []时,它不会被选中.
小智 24
不确定这是mv4的新功能还是先前版本中是否存在.但DropDownListFor包含SelectList构造函数的附加参数.
SelectList(IEnumerable, String, String, Object)
Run Code Online (Sandbox Code Playgroud)
例如:
Html.DropDownListFor( x => x.Countries[ i ], New SelectList(Model.CountryList,"ID","Description",Model.Countries[i]))
Run Code Online (Sandbox Code Playgroud)
对象中ID的国家/地区ID在哪里,是国家/地区名称.CountryListDescription
Nei*_*ham 10
我也是这样.使用foreach循环DropDownListFor时(即在页面上呈现多个选择元素).
我的工作是在控制器而不是视图中设置所选值:如下所示:
在控制器中:
public class FruitList
{
public int? selectedFruit{ get; set; }
public List<SelectListItem> fruits
{
get
{
fruitEntities F = new fruitEntities();
List<SelectListItem> list = (from o in F.Options
select new SelectListItem
{
Value = o.fruitID,
Text = o.fruit,
Selected = o.fruitID == selectedFruit
}).ToList();
return list;
}
}
}
public class ViewModel
{
public List<FruitList> collectionOfFruitLists { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在视图中
<table>
<% for (int i=0; i < Model.collectionOfFruitLists.Count; i++ )
{ %>
<tr>
<td><%: Html.DropDownList("fruitSelectList", collectionOfFruitLists[i].fruits, "Please select...") %></td>
</tr>
<%} %>
</table>
Run Code Online (Sandbox Code Playgroud)
nifty位Selected = o.fruitID == selectedFruit在控制器中,其作用类似于SQL CASE语句; Lance Fisher真的很好地解释了这一点(感谢Lance,你的帖子真的帮了我:)
我知道这个问题有点旧,但我刚刚遇到这个问题,循环遍历一个对象列表,并尝试将值绑定到我的编辑视图中的DropDownListFor(s).
我通过使用其他人为此问题提供的一些先前解决方案的逻辑,克服了内联解决方案的问题.
绑定到我的模型:
@Html.DropDownListFor(model => model.QuestionActions[i].QuestionActionTypeId,
Model.QuestionActionTypes.Select(x => new SelectListItem() { Value = x.Value, Text = x.Text, Selected = (x.Value == Model.QuestionActions[i].QuestionActionTypeId.ToString()) }).ToList(),
"Select Action Type",
new { })
Run Code Online (Sandbox Code Playgroud)
Model.QuestionActionTypes是一个填充在Controller中的SelectList.
| 归档时间: |
|
| 查看次数: |
12661 次 |
| 最近记录: |