embed codebehind results in javascript mvc3

I have a mvc3 view (map.cshtml) that has a javascript section in it.

I have a controller (MapController.cs) that has a function that will return a string.

I need to call the function from within the javascript area of the cshtml page,

and have it render the results directly into the javascript block prior to being processed and displayed.

The whole task is to create a series or infowindows for the google map, and because the data is so scattered around the system, the codebehind function gathers this data, and places it into html code and i need this html code to be injected into the javascript block prior to being displayed.



Here is the actual code block i am currently using...

@functions { private string getContent() { return "testing"; } }

var map;

function initialize() { var myLatlng1 = new google.maps.LatLng(29.64032, -82.363129); var myLatlng2 = new google.maps.LatLng(29.64032, -81.363129);

var myOptions = {
  zoom: 10,
  center: myLatlng1,
  mapTypeId: google.maps.MapTypeId.ROADMAP
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

var test = 'X@{getContent();}';

So - in the var test.... i would like it to read.... Xtesting


You can use jQuery ajax to get data from your Controller actions, in your javascript code.

Include jQuery library in your view (or layout view) and include the below script in your view(map.cshtml).

<script type="text/javascript">


      alert("The value from server is : "+ data);



You need an Action method called GetName in your MapController now to handle this ajax request.

public ActionResult GetName()
   return Content("mvc is awesome");

Is this what you want?

var test = 'X@(getContent())';

Need Your Help

Creating PDF files in PhoneGap using jsPDF

java android cordova jspdf

I'm working on an phonegap android app where I need to generate a pdf file based on user input, so I choosed jsPDF to generate the pdf file.

Loading resources like JQuery and JQuery UI from CDN

jquery grails resources cdn

I am using grails resources 1.1.6 and I am able to load a library from an external host, e.g by doing: