问题:我需要从视图中向用户显示数据。在我的实现中,它打印正确的行数,但数字始终为 0,标题为空。
请求在数据库中创建视图
CREATE VIEW ProductOrderCountView AS
SELECT P.Name AS [Название продукта],
dbo.funcOrderCountProduct(P.Id) AS [Количество заказов]
FROM Products AS P
代表的类
public class ProductOrderCountView
{
[DisplayName("Название продукта")]
public string Name { get; set; }
[DisplayName("Количество заказов")]
public int Count { get; set; }
public static List<ProductOrderCountView> LoadProductOrderViewCount()
{
BakeryContext context = new BakeryContext();
var result = context.Database.SqlQuery<ProductOrderCountView>("SELECT * FROM ProductOrderCountView;").ToList();
return result;
}
}
查看标记
@using Bakery.Models.ForDBView
@model IEnumerable<Bakery.Models.ForDBView.ProductOrderCountView>
@{
ViewBag.Title = "Статистика по количеству проданных продуктов";
}
<h2>Статистика по количеству проданных продуктов</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Count)
</th>
</tr>
@foreach (var item in Model.OrderBy(o => o.Count))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Count)
</td>
</tr>
}
</table>
您的列名称与模型中的字段名称不匹配。更改查询中的列
在