mercredi 11 novembre 2015

How to call Javascript from Razor View

How can I call a Javascript method from a razor view?

At first I tried

@if (TempData["myMessage"] != null)
{ 
    @:displayMessage();
}

and when the page rendered I literally saw "@:displayMessage(); on my HTML page.

So then I tried.

@if (TempData["myMessage"] != null)
{ 
    <script type="text/javascript">displayMessage();</script>
}

Which works if displayMessage function exists on that html page. However in my case displayMessage exists in a seperate javascript file. (Which gets included on this web page)

Aucun commentaire:

Enregistrer un commentaire