0

am working with this array in javascript of marks to show all marks in the map as static work is perfect

 var markers = [
{
    "title": 'Aksa Beach',
    "lat": '31.6227697895779',
    "lng": '-4.998779296875',
    "description": '/www.google.com">Read more</a>'
}];

but if i can add some marks from model in html view is my problem

@foreach (var item in Model) {
    markers.push({
    "title": item.name,
    "lat": item.lat,
    "lng": item.long,
    "description": item.descript
});
}
2
  • I think that serializing objects to JSON is a better way of creating complex data for JS than manually creating them. Commented Sep 29, 2016 at 14:50
  • am not familiar with json Commented Sep 29, 2016 at 22:18

1 Answer 1

3

Wrap the values with single or double quotes. Also since item is a C# variable, you need to use @. You also need to use <text> tag as you are mixing C# code and plain text (js code)

<script>
  var markers = [];
  @foreach (var item in Model) {

   <text>
        markers.push({  "title": "@item.name",
                         "lat": "@item.lat",
                         "lng": "@item.long",
                         "description":"@item.descript"
                   });
   </text>

  }
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

thank you your answer open my mind but my map is gone
May be the lat and long values from your Model collection is not valid. Do a console.log(markers) after the loop and confirm whether that is the expected values

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.