0

Im trying to access a class from code behind in javascript but I am getting the error saying it does'nt exist in this context. This has worked for me before this way.

Here is my code: Code Behind:

public class ReviewData
{
    public int NumberOfReviews { get; set; }
    public double AvgReviewScore { get; set; }
}

This variable has been populated further down

Here is my javascript:

<script type="text/javascript">
   var reviewData = "<%=ReviewData%>"
</script>
6
  • There are a few issues here... firstly you appear to be trying to instantiate a C# class (server-side) in Javascript (client-side), which won't work. You could create your JS object and set its properties in a similar manner though. What is it you want to achieve? Commented Mar 15, 2018 at 14:47
  • Your script makes no sense. You need to access an instance of the C# class, not use the class name. That's just simple OO principles, nothing to do with injecting it into Javascript. And if you want to inject the whole object (not just a single property from it), you need to serialise it to JSON. C# and JS are not just trivially interchangeable, despite some superficial similarities in the syntax. Commented Mar 15, 2018 at 14:51
  • @Diado yes but I am using var at the start instead of ReviewData. That might be the problem. Thanks Commented Mar 15, 2018 at 14:51
  • @Smac Sorry, I didn't quite grasp what you were doing when I wrote my original comment - have edited it Commented Mar 15, 2018 at 14:52
  • 1
    "This variable" - which variable? Do you have a server-side object of type ReviewData, that you want to send to the browser? Commented Mar 15, 2018 at 14:55

2 Answers 2

2

You'll have to use JSON.Net to serialize your class into a JSON string. Inside your class, create a method called "Serialize()" that returns a string and serializes itself.

In the Javascript, you could then write something like: var reviewData = "@ReviewData.Serialize()"

From there, you may have to use Javascript to parse it back into an object from a string... But you can't pass an actual C# class to Javascript. The best you can do is JSON.

Also, this won't work unless you have an object reference of ReviewData in your codebehind.

Sign up to request clarification or add additional context in comments.

Comments

1

Brandon Millers answer is correct if you need to use a list of objects as an array. But if you want to access a single property of a class, you can do this. Declare it as a public variable

public ReviewData reviewData;

protected void Page_Load(object sender, EventArgs e)
{
    reviewData = new ReviewData();
    reviewData.NumberOfReviews = 5;
}

Now you can access the properties of the variable reviewData on the aspx.

<script type="text/javascript">
    var reviewData = '<%=reviewData.NumberOfReviews%>';
</script>

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.