ASP.NET MVC - Razor and JavaScript

How can I put javascript inside an if block?

 @if(TempData["notification-message"] != null) { $('#notification').jnotifyAddMessage({ text: '@TempData["notification-message"]', permanent: false }); } 

I get a bad compile constant value .

+4
source share
1 answer

The $ sign does not signal that Razor is switching to Html mode.

Put the script in the <text> tags:

 @if(!string.IsNullOrEmpty((TempData["notification-message"] as string))) { <text> $('#notification').jnotifyAddMessage({ text: '@TempData["notification-message"].ToString(), permanent: false }); </text> } 
+5
source

Source: https://habr.com/ru/post/1383396/


All Articles