JavaScript function call in MVC 5 Razor view

I saw in another post that you can call a JavaScript function in your razor code, for example:

@:FunctionName() 

For me, although this only outputs the actual words FunctionName()

Here is my view:

 @model PriceCompare.Models.QuoteModel @{ ViewBag.Title = "Quote"; } <h2>Quote</h2> @if (@Model.clarify == true) { // do drop down loic @:ShowClarify(); } else { // fill quote @:ShowQuote(); } <div class="clarify"> You can see the clarify div </div> <div class="quote"> You can see the quote div </div> @section head { <script type="text/javascript"> $(document).ready( function ShowQuote() { $(".quote").show(); }, function ShowClarify() { $(".clarify").show(); } ); </script> } 

Is it because I put it in `@if '? Anyway around this?

+6
source share
2 answers

You need to place your javascript in the <script> , and you need to call the functions within their scope:

 <script type="text/javascript"> $(document).ready( function ShowQuote() { $(".quote").show(); }, function ShowClarify() { $(".clarify").show(); } @if (@Model.clarify == true) { // do drop down loic ShowClarify(); } else { // fill quote ShowQuote(); } ); </script> 
+9
source

If you pass any parameter to a JavaScript function, it must be enclosed in quotation marks ('').

 foreach (var item in files) { <script type="text/javascript"> Attachment(**'@item.FileName'**, **'@item.Size'**); </script> } 
+1
source

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


All Articles