0

there are two parts to this question

number 1:
im having receiving the same issue as in this post
the only diff is that im running in asp.net core v3.1
Dynamic Anonymous type in Razor causes RuntimeBinderException

ive tried the expando solution
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:
apparently this used to work

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

but no longer in asp.net core v3.1
the conversion it allows is ToString()
does anyone know the equivalent in 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/

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

and vice versa with the return

toy
  • 422
  • 1
  • 7
  • 19

1 Answers1

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

Did you mean to pass in expando and not 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"

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • oh yes @tim i meant to pass expando to the PV but i was running the code and it kept crashing out so i bypassed it - i will amend the OP - thanks! – toy Mar 22 '20 at 04:10
  • about the extension method working for you - weird... i will retry - thank you tim. PS i feel like im the onlyl person in the world that cant convert an IDict to expando since every article i read seems to be doing it... – toy Mar 22 '20 at 04:12
  • 1
    Note the code above is slightly different from the code in the link I added - `IDictionary expando = new ExpandoObject();` vs,. `IDictionary expando = new ExpandoObject();` in the link. – Tim Williams Mar 22 '20 at 05:34