[英]'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.