question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to pass ExpandoObject as Model?

See original GitHub issue

Hello,

iam trying to pass a ExpandoObject as Model to the RazorLightEngineBuilder and always receive:

“InvalidOperationException: Can not resolve a content for the template “{0}” as there is no project set.You can only render a template by passing it’s content directly via string using coresponding function overload”

I dont understand the error in this case. Why do I need to set a project when the model changes? The quick and dirty code iam using:

            var engine = new RazorLightEngineBuilder()
              .UseMemoryCachingProvider()
              .Build();

            string template = "Hello, @Model.Name. Welcome to RazorLight repository";
            dynamic expando = new ExpandoObject();
            expando.Name = "John Doe";

            string result = engine.CompileRenderAsync("templateKey", template, expando).Result;

When switching to a stronglyTyped Model it does work.

Any code samples showing me how to handle “dynamic properties”/ExpandoObject would be great!

Thanks alot!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
jzabroskicommented, Mar 27, 2020

@smcl Thanks! Hope my stepping into help Ivan maintain this is helping your company and projects out! This is a great project, so keep on giving this great feedback. Great communities = great open source software = YOU are a great person.

1reaction
smclcommented, Mar 17, 2020

I don’t know what the original issue was, but with 2.0.0-betaX in master what’s happening is that this method is called:

public Task<string> CompileRenderAsync<T>(string key, T model, ExpandoObject viewBag = null)
{
	return _handler.CompileRenderAsync(key, model, viewBag);
}

Which also fails. What @C0d1ngJammer probably wants (if they are still interested) is the IRazorLightEngine.CompileRenderStringAsync() method, so the original snippet would look like this

var engine = new RazorLightEngineBuilder()
  .UseMemoryCachingProvider()
  .Build();

string template = "Hello, @Model.Name. Welcome to RazorLight repository";
dynamic expando = new ExpandoObject();
expando.Name = "John Doe";

string result = engine.CompileRenderStringAsync("templateKey", template, expando).Result;

Both strongly-typed objects and dynamic work fine in this scenario, I think it can be closed - unless @C0d1ngJammer has any objections?

Read more comments on GitHub >

github_iconTop Results From Across the Web

passing ExpandoObject as parameter
I have some sql functions like this (wrote in C#): public void Query(string sqlText, ExpandoObject paramSet) { SqlCommand cmd = new ...
Read more >
ExpandoObject Class (System.Dynamic)
The ExpandoObject class is an implementation of the dynamic object concept that enables ... You can pass instances of the ExpandoObject class as...
Read more >
Pass Data using Session, Tuples, and Dynamic ...
This tutorial teaches you how can you use Session, Tuples, and Dynamic (ExpandoObject) for passing multiple model data into a single view page....
Read more >
Building C# objects dynamically with ExpandoObject
Solution. Use ExpandoObject to create an object that you can add properties, methods, and events to and be able to data bind to...
Read more >
Partial View Usage With Dynamic Expando Object and Tuple
First declare your Exapndo object to declare all your dynamic models in the controller. Here you can review my controller code in which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found