Handlebars Data using JSON string that stored in variable.
-
If I have variable that consist Json string that I got from Json query :
var JsonString;
That JsonString variable consist of this string :
{ "companies" : { "name" : "My company", "address" : "Mainroad 12" } }
This is my source code :
var report = await rs.RenderAsync(new RenderRequest { Template = new Template { Recipe = Recipe.ChromePdf, Engine = Engine.Handlebars, Content = "{{ }}" }, Data = new { } });
My questions, can I use that JsonString variable as datasource?
If it can, how to implementing it and, for example, how to call "name" into Content?Thanks for your reply.
-
You can json parse that json string and pass object into
Data
var report = await rs.RenderAsync(new RenderRequest { Template = new Template { Recipe = Recipe.ChromePdf, Engine = Engine.Handlebars, Content = "{{ }}" }, Data = JObject.Parse(jsonString) });
-
Bless you. Thank you so much Jan :)
I will try it and give feedback on this.
-
I'm using this in Content :
{{companies}}
The output was this :
[object Object],[object Object]
-
Correction, my JsonString variable is a JObject not a string.
-
It seems you are missing the basics of the templating engines.
Please go through the get started tutorial the first
https://jsreport.net/learn/get-started
-
DONE!
I'm using this code working like a charm :)
Data = JObject.Parse(Convert.ToString(JsonString))