DataBinding:'System.Data.Objects.ELinq.InitializerMetadata'

时间:2017-10-05 11:25:54

标签: c# asp.net-mvc linq asp.net-mvc-4 data-binding

在控制器中;

var getSubjectClassList = (SMSEntitiesNew.Subjects).GroupBy(p => p.SubjectName).ToList();
SelectList listA = new SelectList(getSubjectClassList, "SubjectName", "SubjectName");  
ViewBag.getSubjectClassList111111 = listA;

在Index.cshtml;

@Html.DropDownList("getSubjectClassList", ViewBag.getSubjectClassList111111 as SelectList, "Please select Subject", new { @class = "form-control" })

运行Application时,我收到以下错误: -

  

数据绑定:   “System.Data.Objects.ELinq.InitializerMetadata + Grouping`2 [[System.String,   mscorlib,版本= 4.0.0.0,文化=中性,

1 个答案:

答案 0 :(得分:0)

我认为您在这些方面遇到错误:

var getSubjectClassList = (SMSEntitiesNew.Subjects).GroupBy(p => p.SubjectName).ToList();
SelectList listA = new SelectList(getSubjectClassList, "SubjectName", "SubjectName");

Enumerable.GroupBy方法实际返回IEnumerable<IGrouping<TKey, TElement>> Key属性,该属性为分组成员和主机IEnumerable接口提供键值,允许您遍历内部的所有成员群组。它没有为您提供要IEnumerable<Subject>实例转换的SelectList

如果您想在SubjectName中插入唯一的ViewBag列表,以后转换为SelectList,则可以Select和{{1}执行Distinct查询像这样:

OrderBy

类似问题:

Entity Framework ORDER BY issue