2

I am new to ASP MVC and as part of learning i was developing a small map appliation.

below is the code snippet

<ul>  
  <script type="text/javascript">
    @foreach (var item in Model){
            @:AddLocationPin(@item.geo_lat, @item.geo_long, null, 'place 1'); 
           }
</script>
    </ul>

I expect the server to send to browse the something like following

<script type="text/javascript">
AddLocationPin(1.5,-2.9, null, 'place 1'); 
AddLocationPin(11.5,-12.9, null, 'place 1');
 </script>

insted when I use Firebug on Firebfox I see the server sending the following

<script type="text/javascript">
</script>

could anyone of your help me as to what I am doing wrong ? excuse me if this is a very basic question.

tereško
  • 58,060
  • 25
  • 98
  • 150
user1270305
  • 23
  • 1
  • 6

1 Answers1

2

Once inside Razor block you don't need to put @ at every line.

<script type="text/javascript">
    @foreach (var item in Model){
            AddLocationPin(@item.geo_lat, @item.geo_long, null, 'place 1');
           }
  </script>

Razor cheatsheet: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • looking into it further I am wondering if it is not even evaluating the @foreach because I tried to set some value in that loop and it dint mak it either. – user1270305 Jun 15 '12 at 10:38
  • accepting this as an answer as I think the issue is else where, I have posted another question for help on that. Thanks Jakub – user1270305 Jun 15 '12 at 11:06
  • I think this is actually not a correct answer because the OP wanted javascript to be injected into the output (i.e. AddLocationPin is a javascript function that he wants to call). – Miika L. Oct 04 '13 at 12:00