Reputation: 8054
This line
$('.current').val(page + ' of ' + @lastPage);
causes the last parentheses to get underlined with the red squigly that says "Syntax error", and nothing more.
However, THIS line
setPage(@lastPage);
works perfectly fine.
What the heck is up with that? It's causing the scripts in my view to stop working completely, and is rather frustrating.
Could anybody shed some light on the subject, please?
Upvotes: 1
Views: 4821
Reputation: 14747
You're thinking that the Javascript and the (C#) Razor syntax are working together at the same time in the same environment (like how HTML and Javascript do normally). They don't. It's not correct to think of it as "Razor assigning values to jQuery variables" as well.
The key thing to understand here is that the Razor syntax will be evaluated in the server, before it sends a response back to your browser (say, an HTML page). The Javascript will evaluate in the browser, after it's received a response from the server. So it's essentially two environments. A way of thinking about it is that Razor prepares the environment for Javascript to run in on the browser.
Say, for example, your Razor code is like this:
@{
var id = "foo";
}
$('#@id').show();
Your Javascript isn't going to even notice that there was Razor in it, because by the time it gets to it, it would've looked like this:
$('#foo').show();
That's the reason why Javascript can't assign to Razor as well; just because of the fact that by the time Javascript "gets its turn" at evaluating, Razor would've been all done and gone. Your context is now at the client (browser), and your server is done whipping up whatever your request against it would've needed.
Upvotes: 0
Reputation: 1583
@lastPage is going to be pure text when javascript sees it because it is evaluated at server side. You might want to change your jQuery to look more like this:
$('.current').val(page + ' of @lastPage');
OR
$('.current').val(page + ' of ' + '@lastPage');
If @lastPage evaluates to lets say 3 then the rendered JS would look like this
$('.current').val(page + ' of ' + 3);
Which you would want it to look like this
$('.current').val(page + ' of 3');
OR
$('.current').val(page + ' of ' + '3');
Upvotes: 5