gpt4 book ai didi

c# - 在 View 中显示两个模型?

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:50 24 4
gpt4 key购买 nike

我在 Controller 中有两个模型

public ActionResult Search(int? id)
{
var yearSummary = from ti in db.TrainingRecordDBSet
join si in db.StaffInfoDBSet
on ti.StaffId equals si.StaffId
group ti by ti.Year into g
select new {
Year = g.Key,
SHour = g.Sum(ti => ti.Hour)
};

var trainingQ = from t in db.TrainingRecordDBSet.Include("StaffInfo")
where t.StaffId == id orderby t.StaffId, t.Tid
select t;

var list = trainingQ.ToList();

ViewBag.yearSummary = yearSummary;

return View(list);
}

我想同时显示数据库中的“trainingQ”和数据库中的“yearSummary”,其中“trainingQ”已经成功,但是“yearSummary”,我不知道如何在 View 中显示,可以吗我亲爱的 friend 们告诉我该怎么做?

这里是 View 的编码,但不起作用

@var yearSummary = (dynamic) ViewBag.yearSummary;
@foreach (var item in Model)
{
<tr>
<td>
@*var yearSummary = (StaffInfoEditViewModel) ViewBag.yearSummary;*@
@{
Html.DisplayFor(modelItem => item.Shour);
}
</td>
<td>
@{
Html.DisplayFor(modelItem => item.Shour);
}
</td>
</tr>
}
</table>

非常感谢!!

最佳答案

创建一个类(模型)来保存 View 需要的数据..

public class SearchVM
{
public IEnumerable<YearSummary> YearSummary { get; set; }
public IEnumerable<TrainingQ> Training { get; set; }
}

public ActionResult Search(int? id)
{
var model = new SearchVM
{
YearSummary = (from ti in db.TrainingRecordDBSet
join si in db.StaffInfoDBSet
on ti.StaffId equals si.StaffId
group ti by ti.Year into g
select new {
Year = g.Key,
SHour = g.Sum(ti => ti.Hour)
})
.AsEnumerable()
.Select(ys => new YearSummary { Year = ys.YEar, SHour = ys.Shour {)
.ToList(),

TrainingQ = (from t in db.TrainingRecordDBSet.Include("StaffInfo")
where t.StaffId == id orderby t.StaffId, t.Tid
select t)
.ToList()
};

return View(model);
}

关于c# - 在 View 中显示两个模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42638735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com