首页 > 解决方案 > 计算表中每个 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>

标签: c#asp.netasp.net-mvc

解决方案


所有课程都显示相同结果的原因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>
}

推荐阅读