简体   繁体   中英

Syntax error with javascript in razor

I have the following code in a cshtml(razor) file, it has an error in visual studio 2012. If I add quotes around the last argument it will, but this produces the wrong javascript.

This has a syntax error wigaly:

<script type="text/javascript">
@{
    foreach (var i in Model()) {
         <text>JsModel.Add('@i.Name',  @(i.IsDeletable?"true":"false" ) );</text>
    }
} 
</script>

Output, that I want:

<script type="text/javascript">
    JsModel.Add(AName, true );
    JsModel.Add(AnotherName, false );
    …
</script>

This has no syntax error, but has wrong output:

<script type="text/javascript">
@{
    foreach (var i in Model()) {
         <text>JsModel.Add('@i.Name', '@(i.IsDeletable?"true":"false" )' );</text>
    }
} 
</script>

Output:

<script type="text/javascript">
    JsModel.Add(AName, 'true' );
    JsModel.Add(AnotherName, 'false' );
    …
</script>

How can I get it to work without an error?

Can you try this

<script type="text/javascript">
@{
    foreach (var i in Model()) {
         <text>JsModel.Add('@i.Name', @(i.IsDeletable?true:false ) );</text>
    }
} 
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM