Reputation: 219
I have a project where I am passing the data from C# to cshtml by something like this:
public IActionResult Index()
{
ViewData["temp"] = "abc"
return View();
}
This value is then received in the cshtml file where I can read this as
var temp = ViewData["temp"];
I have a typescript file that is then booted from this cshtml file. My question is, how can I use this temp variable in my typescript code. I don't want to create a 'div' element for this temp data and then read its value in typescript as this will not be a clean thing to do from security perspective.
Can you please suggest some way to do it in a proper way?
Upvotes: 3
Views: 2893
Reputation: 23190
In your view returned by the Index
action just choose an existing element (you can choose the body
element if your view contain it) and in that element add an data-*
attribute (you can name it data-temp
) like below :
<SomeElementInMyView data-temp="@temp">...</SomeElementInMyView>
In your Typescript file, I suppose you're using jQuery so get the data value like this:
let dataTemp = $("SomeElementInMyView").data("temp");
Upvotes: 4
Reputation: 114
You can pass it as string variable in html eg.
let typeScriptVariable: string = "@temp";
Upvotes: -1