0

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.

whew...

thanks

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

1
  • What are you going to use the variable test for? Do you intend to have plain text as value for the variable test? Commented Jun 14, 2012 at 15:38

2 Answers 2

1

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">

 $(function(){

    $.get("@Url.Action("GetName","Map")",function(data){
      alert("The value from server is : "+ data);
    });

 });

</script>

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

public ActionResult GetName()
{
   return Content("mvc is awesome");
}
Sign up to request clarification or add additional context in comments.

Comments

0

Is this what you want?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.