简体   繁体   English

计算数据行并在视图ASP.net MVC中显示

[英]Count rows of data and display in view ASP.net MVC

All i'm trying to do is count how many bookings have requested wheelchair access not sure with the best way.... 我要尝试做的就是数出不确定使用最佳方式确定要使用轮椅的预订数量。

using (var context = new TalyllynContext()) 
        {
            var count = context.Bookings.SqlQuery(" SELECT * FROM dbo.Booking Where Wheelchair_Access = 'true' ").Count();
        }
        ViewBag.Count = count;

This is what i want to happen in the SQL, but not sure how to make the view bag Count display the variable or is there a much better solution 这就是我想在SQL中发生的事情,但不确定如何使view bag Count显示变量,还是有更好的解决方案

In the view it can be anything as long as it shows the count value !! 在视图中,它可以是任何东西,只要它显示计数值即可!!

If you are using razor do something like this. 如果您使用剃刀,请执行以下操作。

<p>@ViewBag.Count</p>

That will display the value in the view if you use the code you showed. 如果使用显示的代码,它将在视图中显示该值。

您是否尝试过在using语句之前移动count变量,以便也可以在using之外访问它。

using (var context = new TalyllynContext()) 
    {
        ViewBag.Count = context.Bookings.SqlQuery(" SELECT * FROM dbo.Booking Where Wheelchair_Access = 'true' ").Count();
    }

return View()

是的,将视图移回工作了,谢谢您的救命,我也将其更改为示例

ViewBag.Count = db.Bookings.SqlQuery(" SELECT * FROM dbo.Booking Where Gift_Aid = 'true' ").Count();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM