Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  156] [ 4]  / answers: 1 / hits: 161411  / 12 Years ago, thu, february 14, 2013, 12:00:00

Can someone format the code below so that I can set srcript variables with c# code using razor?



The below does not work, i've got it that way to make is easy for someone to help.



@{int proID = 123; int nonProID = 456;}

<script type=text/javascript>
@{

<text>

var nonID =@nonProID;
var proID= @proID;
window.nonID = @nonProID;
window.proID=@proID;

</text>
}
</script>


I am getting a design time error



enter


More From » c#

 Answers
9

You should take a look at the output that your razor page is resulting. Actually, you need to know what is executed by server-side and client-side. Try this:



@{
int proID = 123;
int nonProID = 456;
}

<script>

var nonID = @nonProID;
var proID = @proID;
window.nonID = @nonProID;
window.proID = @proID;

</script>


The output should be like this:



enter



Depending what version of Visual Studio you are using, it point some highlights in the design-time for views with razor.


[#80233] Tuesday, February 12, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monetm

Total Points: 615
Total Questions: 103
Total Answers: 119

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
monetm questions
Fri, Feb 26, 21, 00:00, 3 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Sun, Jul 26, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;