错误提示:不存在具有键“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" })%>

解决方案 »

  1.   

     <%: Html.DropDownListFor(model => model.CompanyIndustryId, Model.CompanyIndustryList, new { @class = "dropDown_list1", style = "width :132px;", name = "D1" })%>new { id = "CategoryName", name = "CategoryName" 有问题,
    SelectList只有text,value,没有CategoryName,CategoryName
      

  2.   

      public static IEnumerable<SelectListItem> GetRegionList()
            {
                using (EquidityEntitiesConnection db = new EquidityEntitiesConnection())
                {
                    return db.Regions.ToArray().Select(m => new SelectListItem { Text = m.RegionName, Value = m.RegionId.ToString() });
                }
            }注意返回值
    ViewData["SelPostList"] as SelectList
    转换也有问题
      

  3.   

    @{ 
                            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"))
      

  4.   

    action中:                OrganizationBLL obll = new OrganizationBLL(this.LogonModel.ProjectKey);
                    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;" })
      

  5.   

    先看你的前台view是否用了强类型的?
    如果是那就要看是什么强类型了,你贴出你的类型视图才能知道怎么解决
    如果没用到,那么这里就不能用DropDownlistFor这个方法,而需要用DropDownList这个方法
      

  6.   

    DEMO