asp.net-mvc – DropDownListFor在for循环中不选择值
发布时间:2020-05-24 00:28:20 所属栏目:asp.Net 来源:互联网
导读:在我看来 %= Html.DropDownListFor( x = x.Countries[ i ], Model.CountryList )% 在我的控制器 public int[ ] Countries { get; set; }public ListSelectListItem CountryList { get; set; } 当表单发布时,没有问题,下拉列表被填充,用户选择
|
在我看来 <%= Html.DropDownListFor( x => x.Countries[ i ],Model.CountryList )%> 在我的控制器 public int[ ] Countries { get; set; }
public List<SelectListItem> CountryList { get; set; }
当表单发布时,没有问题,下拉列表被填充,用户选择的值被发布.但是当我尝试将已经分配的值的表单加载到Countries []时,它不会被选中. 解决方法我也是一样的当使用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; }
}
在视图中 <table>
<% for (int i=0; i < Model.collectionOfFruitLists.Count; i++ )
{ %>
<tr>
<td><%: Html.DropDownList("fruitSelectList",collectionOfFruitLists[i].fruits,"Please select...") %></td>
</tr>
<%} %>
</table>
这个漂亮的位是控制器中的Selected = o.fruitID == selectedFruit,它像一个SQL CASE语句一样;这真的很好解释了Lance Fisher(感谢兰斯,你的帖子真的帮助我:) (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 如何从WCF客户端拦截raw soap request / respon
- asp.net – 通过GET访问Mvc Web API复杂对象查询
- asp.net – OWIN第二天拒绝身份验证cookie
- asp.net – 如何转换上传的视频并从此文件中获取屏幕截图?
- asp.net – 使用ModalPopupExtender而不必设置TargetContro
- 页面存在时的404 – IIS 5,ASP.NET 4.0
- asp.net-mvc-3 – 淘汰赛和全球化
- asp.net – 会话变量保存在哪里?
- asp.net-core – 如何在aspnet核心应用程序中为静态文件使用
- asp.net-mvc – 测试ASP.NET MVC网站
推荐文章
站长推荐
- asp.net-core – 为什么要在src文件夹中组织项目
- asp.net-mvc – 从MVC版本1迁移后,ASP.NET MVC 2
- asp.net-web-api – 在WebApi2帮助页面中启用文档
- ASP.NET中继器模板,每第N个元素的条件代码
- 如何使用asp.net中的“发布/重定向/获
- asp.net – 是否有支持请求参数连接的URL构建器?
- asp.net – ADO.NET实体数据模型缺少Visual Stud
- asp.net-core – Asp.net Core中的UserHostAddre
- asp.net+js实现批量编码与解码的方法
- 增加ASP.NET站点的executionTimeout和maxRequest
热点阅读
