简体   繁体   English

Viewbag RuntimeBinderException:“对象”不包含定义

[英]Viewbag RuntimeBinderException: 'object' does not contain a definition

How can solve this error to pass data to the view by ViewBag ? 如何解决此错误以通过ViewBag将数据传递到视图?

An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code System.Core.dll中发生类型为“ Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”的异常,但未在用户代码中处理

Additional information: 'object' does not contain a definition for 'name' 附加信息:“对象”不包含“名称”的定义

In Controller 在控制器中

var linq = (from c in db.Catogeries
            join a in db.Articals on c.Id equals a.CatogeryId
            select new  {name=c.Name,title=a.Title });
ViewBag.data = linq;

In View 视野中

@{         
     foreach (var item in ViewBag.data )
     {
          <p>@item.name</p>
     }
}

The simplest way to fix this error creating a class instead of using anonymous object and use strongly-typed model. 解决此错误的最简单方法是创建一个类,而不是使用匿名对象和使用强类型模型。

var linq = (from c in db.Catogeries
        join a in db.Articals on c.Id equals a.CatogeryId
        select new MyCategory { name=c.Name, title=a.Title });

ViewBag.data = linq;

In View : View

foreach (var item in (IEnumerable<MyCategory>)ViewBag.data )
{
     <p>@item.name</p>
}

If you insist about using dynamic you can take a look at these questions: 如果您坚持使用dynamic ,则可以看看以下问题:

Dynamic Anonymous type in Razor causes RuntimeBinderException Razor中的动态匿名类型导致RuntimeBinderException

Simplest Way To Do Dynamic View Models in ASP.NET MVC 3 在ASP.NET MVC 3中做动态视图模型的最简单方法

The problem is actually caused by setting the Viewbag value. 该问题实际上是由设置Viewbag值引起的。 In the process .NET always throws an internal Exception, and then Visual Studio breaks into the debugger. 在此过程中,.NET始终会引发内部Exception,然后Visual Studio进入调试器。

Solution: make Visual Studio break only for exceptions in your own code. 解决方案:使Visual Studio仅针对您自己代码中的异常中断。 Go to menu Tools/Options, select Debugging on the left, and check [v] the option "Enable Just My Code". 转到菜单工具/选项,选择左侧的调试,然后选中[v]选项“启用我的代码”。

here is how I resolved my issue and I hope this may help someone: 这是我解决问题的方式,希望对您有所帮助:

I created a Class: 我创建了一个类:

    public class MyClass
    {
        public decimal EmployeeId { get; set; }
        public string CategoryCode { get; set; }
        public string CategoryDesc { get; set; }
    }
}

At the Controller: 在控制器处:

var query = (from c in context.EmployeeCategory
             where(c.EMPLOYEE_ID == 22)
             join a in context.Categories on c.Category_CODE 
             equals a.Category_CODE
             select new MyClass
                         {
                             EmployeeId = c.EMPLOYEE_ID,
                             CategoryCode = a.Category_CODE,
                             CategoryDesc = a.Category_DESC
                         });
return View(query);

At the View: 在视图中:

@model IEnumerable<MyClass>

@foreach (var item in Model)
{
            @item.EmployeeId <b> - </b>
            @item.CategoryCode <br />
            @item.CategoryDesc <br />
}

You just need to cast it. 您只需要投射它。 The view doesn't have any idea what type the object is so trying to enumerate it won't work until you cast it. 视图不知道对象是什么类型,因此尝试枚举对象将不会起作用,直到您强制转换它为止。 Something like: 就像是:

foreach (var item in (IEnumerable <dynamic>) ViewBag.data) { ... }

Sorry if format is off slightly, trying to do this on phone. 抱歉,如果格式略有关闭,请尝试在电话上执行此操作。

暂无
暂无

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

相关问题 ViewBag RuntimeBinderException:“对象”不包含“ProfileId”的定义 - ViewBag RuntimeBinderException: 'object' does not contain a definition for 'ProfileId' 在视图中提取 ViewBag 动态数据:RuntimeBinderException:“对象”不包含“名称”的定义 - Extract ViewBag Dynamic Data in View : RuntimeBinderException: 'object' does not contain a definition for 'Name' RuntimeBinderException:“对象”不包含“名称”的定义 - RuntimeBinderException: 'object' does not contain a definition for 'Name' RuntimeBinderException:&#39;IReadDataService&#39;不包含&#39;GetBySlug&#39;的定义 - RuntimeBinderException: 'IReadDataService ' does not contain a definition for 'GetBySlug' LINQ加入Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:&#39;对象&#39;不包含以下内容的定义: - LINQ Join Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for “对象”不包含定义 - 'object' does not contain a definition 不包含对象的定义 - Does not contain definition for object “对象”不包含定义 - 'Object' does not contain a definition for 遍历ViewBag时,“字符串”不包含帮助程序的定义 - 'string' does not contain a definition for helper when looping through ViewBag Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“System.__ComObject”不包含“语言”的定义 - Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.__ComObject' does not contain a definition for 'Language''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM