使用 Razor 显示列表<dynamic>
Display List<dynamic> with Razor
我在 Razor 视图中使用动态(匿名对象)列表并显示它。
控制器代码:
var res = (from c in _context.DM_Suivi_Us_Servis
group c by new { c.designation_magasin,c.designation_uf} into g
select new
{
g.Key.designation_magasin,
g.Key.designation_uf,
sum = g.Sum(c => c.nbr_us_servis),
}).ToList();
return View(res);
查看页面(Razor 视图):
@model IEnumerable<dynamic>
@foreach (var item in Model)
{
<tr>
<td>@item.ToString()</td>
</tr>
}
显示此结果:
{ designation_magasin = CO3, designation_uf = NRG, sum = 65 }
{ designation_magasin = INC, designation_uf = NRG, sum = 0 } etc..
但是当我尝试显示每个项目属性时:
@foreach (var item in Model)
{
<tr>
<td>@item.sum</td>
<td>@item.designation_uf</td>
<td>@item.designation_magasin</td>
</tr>
}
我收到以下错误
创建新视图模型:
public class YourName
{
public string DesignationMagasin { get; set; }
public string DesignationUf { get; set; }
public int SumServis { get; set; }
}
并使用投影
var res = (from c in _context.DM_Suivi_Us_Servis
group c by new { c.designation_magasin, c.designation_uf } into g
select new YourName
{
DesignationMagasin= g.Key.designation_magasin,
DesignationUf= g.Key.designation_uf,
SumServis= g.Sum(c => c.nbr_us_servis),
}).ToList();
在视图中添加 @model List<YourName>
Here 您找到了有关 MVC 的更多信息。
我在 Razor 视图中使用动态(匿名对象)列表并显示它。
控制器代码:
var res = (from c in _context.DM_Suivi_Us_Servis
group c by new { c.designation_magasin,c.designation_uf} into g
select new
{
g.Key.designation_magasin,
g.Key.designation_uf,
sum = g.Sum(c => c.nbr_us_servis),
}).ToList();
return View(res);
查看页面(Razor 视图):
@model IEnumerable<dynamic>
@foreach (var item in Model)
{
<tr>
<td>@item.ToString()</td>
</tr>
}
显示此结果:
{ designation_magasin = CO3, designation_uf = NRG, sum = 65 } { designation_magasin = INC, designation_uf = NRG, sum = 0 } etc..
但是当我尝试显示每个项目属性时:
@foreach (var item in Model)
{
<tr>
<td>@item.sum</td>
<td>@item.designation_uf</td>
<td>@item.designation_magasin</td>
</tr>
}
我收到以下错误
创建新视图模型:
public class YourName
{
public string DesignationMagasin { get; set; }
public string DesignationUf { get; set; }
public int SumServis { get; set; }
}
并使用投影
var res = (from c in _context.DM_Suivi_Us_Servis
group c by new { c.designation_magasin, c.designation_uf } into g
select new YourName
{
DesignationMagasin= g.Key.designation_magasin,
DesignationUf= g.Key.designation_uf,
SumServis= g.Sum(c => c.nbr_us_servis),
}).ToList();
在视图中添加 @model List<YourName>
Here 您找到了有关 MVC 的更多信息。