0

I am trying to use razor pages to make an asp.net core v. 2.0 website.

And I want to get an ipaddress from the C# code and use it to call a webapi from javascript. - but I'm stuck on showing an alert from javascript

basically my C# is that simple.

 class Aclass {
  public string Text{get;set;}
 }

and the javascript is like this:

    var text = @Model.Text;
    alert (text);

( and I have the model set for the page..)

does anyone know how to get the Text to show?

Cœur
  • 37,241
  • 25
  • 195
  • 267
kfn
  • 620
  • 1
  • 7
  • 26

2 Answers2

3
<script>
var text = '@(Model.Text)';
    alert (text);
</script>
Farhad Bagherlo
  • 6,725
  • 3
  • 25
  • 47
0

the javascript fix was found here: Using Razor within JavaScript

basically it is a combination of '@ and ( that makes it all work.

var text = '@(Model.Text)'; alert(text);

and boom your flying..

kfn
  • 620
  • 1
  • 7
  • 26