开发者

I cannot access to dynamic object properties in Razor Template

开发者 https://www.devze.com 2023-04-03 01:40 出处:网络
I created a dynamic object like below: dynamic myObject = new { DisplayName = \"Mahesh开发者_如何学C\"

I created a dynamic object like below:

dynamic myObject = new
{
    DisplayName = "Mahesh开发者_如何学C"
}; 



Content = Parse("Main", myObject);

I cannot access to dynamic object properties in Razor Template

Then I parsed it for Razor template. But it doesnt work because of the object cannot access to its properties. What is the problem here ?

Thanks in advance,


I did it by this way and it works. But I want to know why the way I did first is not working

dynamic FooterModel = new ExpandoObject();
FooterModel.DisplayName= "Kaplan";


The anonymous type has some accessibility issues, in that dynamic via the c# provider tries to respect accessibility. An anonymous type in a different module is not accessible (and remember that MVC pages will typically compile into a different module), hence no properties.

IIRC, however, this is actually fixed in a later MVC patch - I seem to remember hitting this when my local machine was a rev higher than our dev-server, meaning: it worked locally on the higher revision, but failed as you describe on the dev-server.

The ExpandoObject does not suffer this because it does not use the c# provider (it implements IDynamicMetaObjectProvider instead), and has no concept of accessibility (it simply maps member-names to the dictionary).

0

精彩评论

暂无评论...
验证码 换一张
取 消