c# - 计算表中每个 id 的 id 数
问题描述
我有两张桌子。一个是预订,另一个是课程。课程可以有很多预订。我想计算每门课程的预订数量并将其传递给 ViewBag。所以我想统计每门课程有多少用户申请。
这条线计算了预订的总数,但我不知道如何为每门课程做到这一点。
ViewBag.Counter = db.Bookings.Count();
这一行将所有课程都带到我的索引页面。
IEnumerable<Course> courses = cr.GetCourses();
return View(courses.ToList());
这是在做这项工作的控制器。我尝试了 2 个 foreach 循环,但无法正常工作。
public ActionResult Index()
{
Booking booking = new Booking();
Course course = new Course();
List<Course> listCourses = new List<Course>();
List<Booking> bookings = new List<Booking>();
foreach (var item in listCourses)
{
int id = course.CourseId;
foreach (var item1 in bookings)
{
ViewBag.Counter = db.Bookings.Where(x => x.CourseId == id).Count();
}
}
IEnumerable<Course> courses = cr.GetCourses();
return View(courses.ToList());
}
我希望有一些用户申请我的索引页面上列出的每门课程。我没有任何异常或错误,我的视图中也没有显示任何内容。当我使用时,ViewBag.Counter = db.Bookings.Count();
我得到了申请所有可用课程的用户总数。视图上的模型是与预订相关的课程模型,具有一对多的关系。
这是我的 ActionResult 的视图。
@model IEnumerable<Entities.Course>
@{
ViewBag.Title = "AlgebraSchoolApp";
}
<h2>Dobrodošli!</h2>
<p>
Da bi se prijavili na neki od naših tečajeva kliknite na link prijave:
<button>
@Html.ActionLink("Prijava","Create","Booking")
</button>
</p>
<h2> Svi tečajevi</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CourseName)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayName("Broj polaznika")
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CourseName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@ViewBag.Counter
</td>
</tr>
}
</table>
<h2>Slobodni tečajevi</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CourseName)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
</tr>
@foreach (var item in Model.Where(x => x.Full == false))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CourseName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
</tr>
}
</table>
解决方案
所有课程都显示相同结果的原因ViewBag.Counter
是因为每次都被覆盖ViewBag.Counter = db.Bookings.Where(x => x.CourseId == id).Count();
。
如果我了解您的问题,可以通过创建一个新的视图模型来存储课程所需的信息并将其返回到您的视图中来解决。例如:
public class CourseViewModel
{
public string CourseName { get; set; }
public string Description { get; set; }
//etc
public int BookingCount { get; set; }
}
控制器:
public ActionResult Index()
{
var courses = cr.GetCourses();
var courseViewModels= new List<CourseViewModel>();
foreach (var course in courses )
{
var bookingCount = db.Bookings.Where(x => x.CourseId == course.CourseId).Count();
courseViewModels.Add(new CourseViewModel{
CourseName = course.CourseName, //Add all the vm properties
BookingCount = bookingCount
});
}
return View(courseViewModels);
}
在视图中:
@model IEnumerable<CourseViewModel>
@/*...*/
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CourseName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookingCount )
</td>
</tr>
}
推荐阅读
- javascript - 澄清 appendChild
- java - 在java中获取处理json对象的错误消息
- python - 如何绘制元素计数和添加注释
- xml - “XML 任务”xslt 错误以非规范化 XML 文件
- flutter - Flutter:如何创建这个可拖动的底部小部件?
- mysql - 如何避免mysql查询系统锁定
- photon - 光子。在 Android 上重启 WI-FI 后 OnRoomListUpdate 不起作用
- javascript - 无法在 Angular js 中创建选项卡
- python - 如何以数据类表示法将类实例添加到类变量中?
- opencart-3 - Opencart - 同一产品的多个附属公司重定向选项代替“添加到购物车”