我正在使用asp.mvc构建一些ERP,我对MVC有点新意。我试图找到如何在表格中使用Count
方法foreach行(需要从DB计算每个公司有多少产品)。
<td>
@Html.DisplayFor(modelItem => item.CountBusinessOffers(item.bId))
</td>
计数方法
public int CountBusinessOffers(int id)
{
DbHelper.ConnectRemoteDB();
DbHelper.products_collection
= DbHelper.Database.GetCollection<OfferItem>("products");
var filter_byId = Builders<OfferItem>.Filter.Eq("bId", id);
var filter_byDate = Builders<OfferItem>.Filter.Eq("isActive", true);
var filter_byActive = Builders<OfferItem>.Filter.Gte("limitDate", DateTime.Now);
var filter = filter_byId & filter_byActive & filter_byDate;
var document = DbHelper.products_collection.Find(filter).Count();
int numberOfItems = Convert.ToInt32(document);
return numberOfItems;
}
但是我收到了一个错误:
System.InvalidOperationException:'模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式。'