asp.net mvc - In Javascript in a MVC Razor, replace a keyword with a value from the model -


when have javascript on mvc razor view possible model variable replacement in middle of multipart statement.

to clarify have code

<script>  function drawchart() {     $.post('@url.content("~/home/getdataassets")', function (d) {         ...         var chart = new google.visualization.linechart(document.getelementbyid('chart_div'));         chart.draw(data, options);     }); }; 

where want type of graph displayed come model. chart line have

var chart = new google.visualization.@model.charttype(document.getelementbyid('chart_div')); 

where linechart has been replaced @model.charttype

this gives error

charttype being used method

is there way make substitution?

as @stephen-muecke said razor code parsed on server side , javascript code on client side.

if model.charttype returns string, i.e. linechart, need output paranthesis , content of (...) text.

to have add @{<text> ... </text>} output plain text, below

var chart = new google.visualization.@model.charttype@{<text>(document.getelementbyid('chart_div'));</text>} 

the result of client side be, example

var chart = new google.visualization.linechart(document.getelementbyid('chart_div')); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -