Invalid character warning in MVC razor view

Trying to get the following result in javascript:

if (someCallback) someCallback(); 

Where "someCallback" is the value of the string from my view model.

I tried in my opinion:

 $("#btnOK").click(function() { @{ if (!string.IsNullOrEmpty(Model.JavascriptCallback)) { <text> if (@(Model.JavascriptCallback)) @(Model.JavascriptCallback)(); </text> } } }); 

This prints out what I want, but I get a brace warning in "@ {" indicating "Invalid character".

Using @if also works, but with even more warnings:

 $("#btnOK").click(function() { @if (!string.IsNullOrEmpty(Model.JavascriptCallback)) { <text> if (@(Model.JavascriptCallback)) @(Model.JavascriptCallback)(); </text> } }); 

Is there a way to achieve this without receiving a warning?

+4
source share
1 answer

You can turn off alerts on the Razor page by placing the following in a view:

 @{ #pragma warning disable } 
0
source

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


All Articles