错误提示:不存在具有键“EntityPost.CategoryName”的“IEnumerable<SelectListItem>”类型的
Controller里面
IEnumerable<SelectListItem> SelPostList = from c in _context.PostCategory.ToList()
select new SelectListItem
{
Value = c.CategoryID.ToString(),
Text = c.CategoryName
};
ViewData["SelPostList"] = SelPostList;
前台<%= Html.DropDownListFor(m => m.EntityPost.CategoryName, ViewData["SelPostList"] as SelectList, new { id = "CategoryName", name = "CategoryName" })%>
Controller里面
IEnumerable<SelectListItem> SelPostList = from c in _context.PostCategory.ToList()
select new SelectListItem
{
Value = c.CategoryID.ToString(),
Text = c.CategoryName
};
ViewData["SelPostList"] = SelPostList;
前台<%= Html.DropDownListFor(m => m.EntityPost.CategoryName, ViewData["SelPostList"] as SelectList, new { id = "CategoryName", name = "CategoryName" })%>
解决方案 »
- Laber控件对已连接数据表的DropDownList控件同行不同列的数据实时显示问题
- 两个DateTime,如何只比较二者的年和月?
- An unhandled exception has occurred
- PetShop安装 出现错误 错误代码2869
- 关于iframe中form认证问题,急!!!!!!
- 请问怎么保存网站(chs.gotdotnet.com)上的文章(包括相互间的链接)到本地硬盘?
- C# 是不是不能 操作 GIF图片??
- 我的DataGrid怎么在多出来了一个空白的第一列??
- 用C#写的随机数问题
- 求“客户端判断TextBox不能为空值的脚本”。
- 在asp.net下,用c#开发,如何实现模糊查询
- Tables 当前上下文中不存在名称“Tables”
SelectList只有text,value,没有CategoryName,CategoryName
{
using (EquidityEntitiesConnection db = new EquidityEntitiesConnection())
{
return db.Regions.ToArray().Select(m => new SelectListItem { Text = m.RegionName, Value = m.RegionId.ToString() });
}
}注意返回值
ViewData["SelPostList"] as SelectList
转换也有问题
Dictionary<int, string> list = new Dictionary<int, string>();
list.Add(0, "全部");
list.Add(1, "使用");
list.Add(2, "终止");
}
@Html.DropDownListFor(m => m.CandidateItems.Status, new SelectList(list, "key", "value"))
List<OrganizationEn> orgList = obll.GetScopeSonModeList(enobj.fOrgId.Value);
if (orgList != null && orgList.Count > 0)
{
List<SelectListItem> list = new List<SelectListItem>();
foreach (OrganizationEn oen in orgList)
{
SelectListItem slitem = new SelectListItem();
slitem.Text = oen.fOrgName;
slitem.Value = oen.fOrgId.ToString();
list.Add(slitem);
} ViewData["OrgList"] = list;
}
页面:
@Html.DropDownList("fOrgList", ViewData["OrgList"] as List<SelectListItem>, null, new { @style = "width:168px;" })
如果是那就要看是什么强类型了,你贴出你的类型视图才能知道怎么解决
如果没用到,那么这里就不能用DropDownlistFor这个方法,而需要用DropDownList这个方法