简体   繁体   English

“对象”不包含定义<property>在asp.net核心3.1中

[英]'object' does not contain a definition for <property> in asp.net core 3.1

there are two parts to this question这个问题有两个部分

number 1: 1号:
im having receiving the same issue as in this post我收到了与这篇文章相同的问题
the only diff is that im running in asp.net core v3.1唯一的区别是我在 asp.net core v3.1 中运行
Dynamic Anonymous type in Razor causes RuntimeBinderException Razor 中的动态匿名类型导致 RuntimeBinderException

ive tried the expando solution我试过expando解决方案
but i still get the 'object' does not contain a definition for 'myproperty' err但我仍然得到“对象”不包含“我的财产”错误的定义

heres my code继承人我的代码
in controller :控制器中

        var obj = new { newRoundNumber = maxround + 1 };
        IDictionary<string, object> dict = new ExpandoObject();
        dict.Add("p", obj);
        var expando = StaticFunctions.ToExpando(dict);
        return PartialView("_RoundsPartial", expando);

in partialview :局部视图中

@{
var p =  Model.p;
var newRound = p.newRoundNumber;   <--- fails

for (var r = 1; r <= newRound; r++)
{
    <div>@r</div>
}

} }

can someone tell me whats up?谁能告诉我怎么了?

number 2: 2号:
apparently this used to work显然这曾经有效

new{
  Project = projectInfo,
  WorkItem = workitem
}.ToExpando());

but no longer in asp.net core v3.1但不再在asp.net core v3.1
the conversion it allows is ToString()它允许的转换是 ToString()
does anyone know the equivalent in asp.net core?有没有人知道asp.net core 中的等价物?

UPDATE:更新:
there was a comment response to try this link:有评论回复可以尝试此链接:
https://sebnilsson.com/blog/convert-c-anonymous-or-any-types-into-dynamic-expandoobject/ https://sebnilsson.com/blog/convert-c-anonymous-or-any-types-into-dynamic-expandoobject/

in short:简而言之:

using System.Dynamic;
public static ExpandoObject ToExpandoObject(this object obj)
{
    IDictionary expando = new ExpandoObject();

    foreach (PropertyDescriptor property in 
        TypeDescriptor.GetProperties(obj.GetType()))
    {
        expando.Add(property.Name, property.GetValue(obj));
    }

    return (ExpandoObject) expando;
}

currently this returns in error on this line:目前这在这一行返回错误:

    IDictionary expando = new ExpandoObject();

with:和:
cannot implicitly convert type ExpandoObject to IDictionary不能将类型 ExpandoObject 隐式转换为 IDictionary

and vice versa with the return反之亦然

var expando = StaticFunctions.ToExpando(dict);
return PartialView("_RoundsPartial", dict);

Did you mean to pass in expando and not dict ?您的意思是传入expando而不是dict吗?

The extension method worked for me:扩展方法对我有用:

public static System.Dynamic.ExpandoObject ToExpandoObject(this object obj)
{
    IDictionary<string, object> expando = new ExpandoObject();
    foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(obj.GetType()))
    {
        expando.Add(property.Name, property.GetValue(obj));
    }
    return (ExpandoObject)expando;
}

...but I got the same error "object does not contain a definition" ...但我得到了同样的错误“对象不包含定义”

暂无
暂无

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

相关问题 ASP.NET Core 3.1“‘用户’不包含‘FindFirstValue’的定义” - ASP.NET Core 3.1 "'User' does not contain a definition for 'FindFirstValue'" ASP.NET Core:IConfigurationBuilder 不包含 AddAzureKeyVault 的定义 - ASP.NET Core: IConfigurationBuilder Does Not Contain Definition For AddAzureKeyVault 带有 MVC iapplicationbuilder 的 ASP.NET Core 不包含 usemvc 的定义 - ASP.NET Core with MVC iapplicationbuilder does not contain a definition for usemvc .net core 3.1 Blazor WebAssembly:不包含“LoginMode”的定义 - .net core 3.1 Blazor WebAssembly: does not contain a definition for "LoginMode" DbContextOptionsBuilder&#39;不包含&#39;usesqlserver&#39;的定义并且在asp.net core 2.2中没有扩展方法&#39;usesqlserver&#39;? - DbContextOptionsBuilder' does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver' in asp.net core 2.2? ASP.NET 内核 - 如何解决“ICollection”<approvalattachment> ' 不包含 'FilePath' 的定义并且没有可访问的扩展方法</approvalattachment> - ASP.NET Core - How to resolve 'ICollection<ApprovalAttachment>' does not contain a definition for 'FilePath' and no accessible extension method “IServiceCollection”不包含“AddAWSService”的定义,并且在asp.net core 2.2 中没有可访问的扩展方法“AddAWSService”错误? - 'IServiceCollection' does not contain a definition for 'AddAWSService' and no accessible extension method 'AddAWSService' error in asp.net core 2.2? int不包含Getawaiter + asp.net核心Web api异步的定义 - int does not contain a definition for Getawaiter + asp.net core web api async ASP.NET Core API 获取错误:&#39;EntityEntry<Vehicle> &#39; 不包含定义 - ASP.NET Core API Get error: 'EntityEntry<Vehicle>' does not contain a definition 'console'在asp.net 5控制台应用程序中不包含'ReadKey'的定义 - 'Console' does not contain a definition for 'ReadKey' in asp.net 5 console App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM