Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed c# code in javascript on cshtml MVC3 Razor page

how would I do the equivalent of this embedded in JavaScript on an MVC2 aspx page:

if (('<%= Model.SomeFunctionEnabled %>' == 'True') and also a whole function code block on a Razor view page (cshtml) in MVC3?

Something like :

@{
    foreach(var d in Model.Employees)
    {
        ....
    }
}

Which works fine when embedded in the HTML part of the view page. Thanks

like image 327
stan4th Avatar asked Mar 04 '11 14:03

stan4th


People also ask

What is embedded C code?

Embedded C is a set of language extensions for the C programming language by the C Standards Committee to address commonality issues that exist between C extensions for different embedded systems.

Is embedded C easy?

In embedded system programming C code is preferred over other language. Due to the following reasons: Easy to understand. High Reliability.


1 Answers

Why testing on the client side when you could do this on the server side and include the javascript to act accordingly if the test succeeds:

<script type="text/javascript">
    @if (Model.SomeFunctionEnabled) {
        <text>
        // Put your javascript code here
        alert('the function is enabled');
        </text>
    }
</script>
like image 83
Darin Dimitrov Avatar answered Oct 13 '22 11:10

Darin Dimitrov