0

In c# I have one method

public IEnumerable<Office> PointOffices;

private void PrepareMap(IEnumerable<Office> tdMaps)
  {
      var pointOffices = tdMaps as Office[] ?? tdMaps.ToArray();
      if (tdMaps == null || !pointOffices.Any()) return;
      PointOffices = pointOffices;
 }

In JavaScript I want to get each element of Office and generate different html with attributes of Office class

I tried to do like this

var pointOffices =<%= PointOffices %>;

but it gives me error: Unexpected token ]

Do I need to serialise the class or the method to get it in js ? how can I realize this?

2
  • possible duplicate of Pass IEnumerable List to javascript Commented Mar 21, 2014 at 10:46
  • Why not create HTML element on server side, as you already have data there ? Other possibility is to make a web service for getting data and then from browser js make ajax call to get the data from this REST web service as JSON and then js in browser consumes this and adds new HTML elements. Commented Mar 21, 2014 at 10:48

1 Answer 1

1

Do I need to serialise the class or the method to get it in js?

Yep!

Check out JSON.NET.

So in your example you should be able to do something like this

public string SerializedPointOffices
{
    get { return JsonConvert.SerializeObject(this.PointOffices); }
}
Sign up to request clarification or add additional context in comments.

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.