这是我的行动代码
public class personnelViewModel{
public Guid PKPersonelID { get; set; }
public string name { get; set; }
public string family { get; set; }
public int usageCount { get; set; }
}
var model = from personnel in DB.RFH_Personnel select new personnelViewModel
{
PKPersonelID = personnel.PKPersonelID,
name = personnel.Name,
family = personel.Family
}
freach(var item in model)
item.usageCount = DB.RFH_Service.Count(d => d.FKPersonelID == item.PKPersonelID);
return View(model);
这是我的观点:
@model IEnumerable<RefahiWeb.Models.personnelViewModel>
@{
var grid = new WebGrid(source: Model, rowsPerPage: 15, canPage: true, canSort: true);
}
@grid.GetHtml(tableStyle: "table tablesorter table-bordered table-hover request-status",
mode: WebGridPagerModes.All,
columns: grid.Columns(
grid.Column("name", "name", canSort: false),
grid.Column("family", "family", canSort: false),
grid.Column("usageCount", "usageCount", canSort: false)
)
)
问题是usageCount始终设置为零,并且在froeach循环中设置的值不会更改usageCount的值。提前感谢任何帮助。
答案 0 :(得分:0)
您的问题是您没有注意到model
只是一个查询。当您浏览model
时,请更改所有内容,但在返回时,您只需重新执行 model
存储的查询。所以要解决这个问题,你在这里至少有两个选择:
在ToList()
之后附加model
,然后一切都应该有效:
var model = from personnel in DB.RFH_Personnel
select new personnelViewModel {
PKPersonelID = personnel.PKPersonelID,
name = personnel.Name,
family = personel.Family
}.ToList();
在查询中分配usageCount
权限,如下所示:
var model = from personnel in DB.RFH_Personnel
select new personnelViewModel {
PKPersonelID = personnel.PKPersonelID,
name = personnel.Name,
family = personel.Family,
usageCount = DB.RFH_Service.Count(d => d.FKPersonelID == personnel.PKPersonelID)
};
注意:我不知道您的View
方法接受了什么,但当您追加ToList()
时,您的模型将是List...
,您应该接受考虑到相应地修改你的代码。