我对数据进行了分组,我想将这些数据与模型一起发送到视图。如何将属性写入视图模型工作计划类public class WorkPlan:IEntity{ public int WorkPlanId { get; set; } public int PlanNumber { get; set; } public string Day { get; set; } public string Lesson { get; set; } public string Description { get; set; } public bool IsActive { get; set; }}工作计划视图模型public class WorkPlanViewModel:BaseViewModel{ public List<WorkPlan> WorkPlans { get; set; } public WorkPlan WorkPlan { get; set; }}行动public IActionResult Index(){ WorkPlanViewModel model=new WorkPlanViewModel(); model.Message=new Message(); var allWorkPlan = workPlanService.GetAll(); var groupPlan = allWorkPlan.GroupBy(p => p.PlanNumber) .Select(p => new {PlanNumber=p.Key, Day= p.GroupBy(b => b.Day).ToList() } ).ToList(); return View(model);}groupPlan 是我的分组数据
1 回答
慕容708150
TA贡献1831条经验 获得超4个赞
您可以创建一个反映该匿名类型的类,并将其作为属性添加到您的视图模型中。
控制器/操作代码 - 请注意new GroupPlan以下代码Select
var vm = new MyViewModel();
vm.GroupPlans = allWorkPlan.GroupBy(p => p.PlanNumber)
.Select(p => new GroupPlan { PlanNumber = p.Key, Day = p.GroupBy(b => b.Day).ToList() }).ToList();
群组计划类
public class GroupPlan
{
public int PlanNumber { get; set; }
public List<IGrouping<string, WorkPlan>> Day { get; set; }
}
视图模型类
public class MyViewModel
{
public List<GroupPlan> GroupPlans { get; set; }
}
- 1 回答
- 0 关注
- 79 浏览
添加回答
举报
0/150
提交
取消