asp.net-mvc – 将mvc3中的下拉列表绑定到字典?
发布时间:2020-12-16 03:41:44 所属栏目:asp.Net 来源:网络整理
导读:我在这里错过了什么? 视图模型: public class ViewModel{ public IDictionaryint,string Entities { get; set; } public int EntityId { get; set; }} 控制器: public override ActionResult Create(string id) { ViewModel = new ViewModel(); IEnumerab
我在这里错过了什么?
视图模型: public class ViewModel { public IDictionary<int,string> Entities { get; set; } public int EntityId { get; set; } } 控制器: public override ActionResult Create(string id) { ViewModel = new ViewModel(); IEnumerable<Entity> theEntities = (IEnumerable < Entity >)db.GetEntities(); model.Entities= theEntities.ToDictionary(x => x.Id,x => x.Name); return View(model); } 视图: <div class="editor-field">@Html.DropDownListFor(model => model.EntityId,new SelectList(Model.Entities,"Id","Name"))</div> </div> 错误:
解决方法
KeyValuePair具有Key和Value属性.您最好将实体声明为类型IEnumerable< Entity>,然后您的视图将按原样运行:
public class ViewModel { public IEnumerable<Entity> Entities { get; set; } public int EntityId { get; set; } } 或者,如果您确实需要使用词典<>,请更改您的视图: <div class="editor-field"> @Html.DropDownListFor(model => model.EntityId,"Key","Value")) </div> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-4 – 使用angularjs和ASP.NET MVC
- ASP.net c#替换字符串不起作用
- asp.net – 什么时候应该使用Page.DataBind()和Control.Dat
- 单声道和ASP.NET身份验证
- ASP.NET C#将用户添加/更新到角色
- asp.net-mvc – $.ajax({async:false})请求仍然异步发射?
- asp.net – IIS7和IE8 – 在相同版本的IE8上呈现不同的页面
- asp.net-mvc – 如何在发布MVC应用程序时包含自定义文件夹?
- 如何阻止IIS 8停止空闲的ASP.NET应用程序?
- ASP.NET vNext Core CLR缺少type.IsPrimitive
推荐文章
站长推荐
热点阅读