Call JavaScript function from ASP.NET Razor

Viewed 5827

I am writing Razor code in a view, and need to call a JavaScript function with the JSON that is rendered by the Razor code:

// JavaScript function
function buildTemplate(currentTemplate) {
    alert('hello world');
}

@* Build out entire Template structure in JavaScript *@
@foreach (Template template in Model.Templates)
{            
    buildTemplate(JsonConvert.SerializeObject(template));        
}

For some reason, Razor thinks that buildTemplate is a C# method it can't find, and it errors out.

Has anyone done this before? Any advice?

Thanks,

Philip

2 Answers
Related