Cody
Cody

Reputation: 8944

Using <% asp tags in javascript not working?

I can't get the following to work:

var percent = <% Model.Percent; %>

I am sending a Model to the view from the controller...I'm getting the error:

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Am I being extremely stupid?

Upvotes: 1

Views: 87

Answers (1)

Kirk Woll
Kirk Woll

Reputation: 77546

Replace:

var percent = <% Model.Percent; %>

With:

var percent = <%= Model.Percent %>

The <% nugget simply means, "run this code as a C# statement". It doesn't actually render any value. <%= on the other hand evaluates the C# expression, and converts it to a string and renders it. This way, it'll print out the percent in your javascript.

Upvotes: 5

Related Questions