简体   繁体   English

匿名类的 ASP.NET MVC ViewBag 列表在 Count() 方法上引发错误

[英]ASP.NET MVC ViewBag list of anonymous class throws error on Count() method

I have a serverside code where I'm returning a list of anonymous class from the database:我有一个服务器端代码,我从数据库中返回一个匿名类列表:

    public ActionResult DisplayMap()
    {
        ViewBag.Checkins = (from locationUpdate in db.LocationUpdates
                            select new
                            {
                                locationUpdate,
                                locationUpdate.User
                            }).ToList();
        return View();
    }

At the Razor page, I want to get the count of this list:在 Razor 页面上,我想获取此列表的数量:

@if (ViewBag.Checkins.Count() > 0)
{ ... }

However, it throws an error:但是,它会引发错误:

An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred 
in System.Core.dll but was not handled in user code.

Additional information: 'object' does not contain a definition for 'Count'

When I type ViewBag.Checkins in immediate window, I get:当我在即时窗口中输入ViewBag.Checkins时,我得到:

ViewBag.Checkins
{System.Collections.Generic.List<<>f__AnonymousType6<MY_APP.LocationUpdate,MY_APP.User>>}
    [0]: { locationUpdate = {System.Data.Entity.DynamicProxies.LocationUpdate_4532566693B61EF657DDFF4186F1D6802EA1AC8D5267ED245EB95FEDC596E129}, User = {System.Data.Entity.DynamicProxies.User_816C8A417B45FE8609CD1F0076A5E6ECBAB0F309D83D2F8A7119044B1C6060CF} }

The Checkins object is indeed a List , and the data is correct. Checkins对象确实是一个List ,并且数据是正确的。 I've tried Count , Length too (without method call, just as properties) but no luck.我也试过CountLength (没有方法调用,就像属性一样)但没有运气。 What am I doing wrong?我究竟做错了什么?

ViewBag is dynamic , while Count is an extension method, which isn't supported dynamically (it has to be bound at compile time ). ViewBagdynamic ,而Count是一个扩展方法,它不是动态支持的(必须在编译时绑定)。

You can either cast to an IEnumerable<dynamic> :您可以转换为IEnumerable<dynamic>

@if (((IEnumerable<dynamic>)ViewBag.Checkins).Count() > 0)

or use the static method directly:或者直接使用静态方法:

@if (Enumerable.Count(ViewBag.Checkins) > 0)

Or create a strongly-typed model with a Checkins property and avoid ViewBag altogether.或者创建一个强类型的模型Checkins财产,避免ViewBag干脆。

EDIT编辑

Since you're just wanting to check if the count is greater than 0, Any is more appropriate (and may save some processing time depending on the scenario):由于您只想检查计数是否大于 0,因此Any更合适(并且可能会根据场景节省一些处理时间):

@if (Enumerable.Any(ViewBag.Checkins))

The viewbag is dynamic which make it an anonymous type generated as internal. viewbag 是动态的,这使它成为内部生成的匿名类型。 It's better that you use view models instead.最好使用视图模型。

and then call the view with its model, do something like this:然后用它的模型调用视图,做这样的事情:

public class MyViewModel{
LocationUpdate LocationUpadte{get;set;}
User User{get;set;}
}

public ActionResult DisplayMap()
{
        var model = (from locationUpdate in db.LocationUpdates
                            select new MyViewModel
                            {
                                locationUpdate,
                                locationUpdate.User
                            }).ToList();
        return View(model);
}

then in you razor view然后在你的剃刀视图中

@Model.Count() will return the expected value @Model.Count()将返回预期值

You need to cast your object since the viewbag is dynamic.由于 viewbag 是动态的,因此您需要投射您的对象。 For example:例如:

var list = ViewBag.List as List<int>();
list.Count();

你可以这样做:

- @if(ViewBag.Checkins.Count > 0)

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

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