Asp.net mvc 无法在MVC 3.0中强制转换类型为的对象
我是新的MVC 3.0 Razor。我正在使用LINQ获取配置文件的详细信息 在我正在使用的控制器中Asp.net mvc 无法在MVC 3.0中强制转换类型为的对象,asp.net-mvc,asp.net-mvc-3,linq,razor,Asp.net Mvc,Asp.net Mvc 3,Linq,Razor,我是新的MVC 3.0 Razor。我正在使用LINQ获取配置文件的详细信息 在我正在使用的控制器中 var q = from m in networks.Entities join o in networks.EntityTypes on m.EntityTypeIdFk where m.Status == 1 orderby m.EntityName
var q = from m in networks.Entities
join o in networks.EntityTypes on m.EntityTypeIdFk
where m.Status == 1
orderby m.EntityName
select new { m.EntityName,m.EntityDescription,o.EntityTypeName };
ViewBag.EntitiesName = q.ToList();
关于我使用的视图
@foreach (var res in ViewBag.EntitiesName)
{
<tr>
<td>@res.EntityName</td>
<td>@res.EntityDescription</td>
<td>@res.EntityTypeName</td>
</tr>
}
@foreach(ViewBag.EntitiesName中的变量res)
{
@res.EntityName
@实体描述
@res.EntityTypeName
}
在@res中,我得到了结果数组。但仍然出现错误“无法强制转换类型的对象”
请帮我解决这个问题
谢谢,
萨洛尼
鉴于
@model List<MVCSAMPLE.Models.Entities>
@foreach (var item in Model)
{
<tr>
<td>@item.EntityName</td>
<td>@item.EntityDescription</td>
<td>@item.EntityTypeName</td>
<td>@item.IndustryName</td>
<td>@item.Name</td>
</tr>
}
@型号列表
@foreach(模型中的var项目)
{
@item.EntityName
@item.EntityDescription
@item.EntityTypeName
@item.IndustryName
@项目名称
}
举个例子。尝试指定ur列表的类型。
另一种方法是在ur模型和视图中增加列表类型属性
@model List<MVCSAMPLE.Models.MyModel>
@foreach (var item in Model.PropertyWhithListType)
{
<tr>
<td>@item.EntityName</td>
<td>@item.EntityDescription</td>
<td>@item.EntityTypeName</td>
<td>@item.IndustryName</td>
<td>@item.Name</td>
</tr>
}
@型号列表
@foreach(Model.PropertyWhithListType中的var项)
{
@item.EntityName
@item.EntityDescription
@item.EntityTypeName
@item.IndustryName
@项目名称
}
我通过使用
select new MVCSAMPLE.Models.Entities
{
EntityName = m.EntityName,
EntityDescription = m.EntityDescription,
EntityTypeName = o.EntityTypeName
}
感谢大家。无法强制转换类型为“f_uuAnonymousType3”的对象5[System.String,System.String,System.String,System.String,System.String]“您是否已将模型传递到视图?是的,我正在视图顶部使用@model MVCSAMPLE.Models.Entities我收到此错误传递到字典中的模型项的类型为”System.Collections.Generic.List
1[f_uAnonymousType3
5[System.String,System.String,System.String,System.String,System.String,System.String]]”,但此词典需要类型为“System.Collections.Generic.List`1[MVCSAMPLE.Models.Entities]”的模型项。确定,然后尝试列出model=q.ToList();=)我收到错误“无法从'System.Linq.IQueryable'转换为'System.Collections.Generic.IEnumerable'”,或者可能由您的Linq返回实体的IList。其思想是控制器和视图中ur模型的类型必须相同。在大多数情况下,尽量避免使用ViewBag或ViewData。我认为这只是简单的typestry用来替换select new{m.EntityName,m.EntityDescription,o.EntityTypeName};在-->上选择m
select new MVCSAMPLE.Models.Entities
{
EntityName = m.EntityName,
EntityDescription = m.EntityDescription,
EntityTypeName = o.EntityTypeName
}