Mark
Mark

Reputation: 1687

Razor Inline code generating Compiler Error

I am trying to dynamically set the content of a cell using the KendoGrid Content Template and inline Razor but I am getting all sorts of errors.

"<table><tr>" + 
"<td style='color:yellow; width:200px;height:13px;padding-top:0px; margin-top:0px;text-align:center;' class='audiogrid'>" +
@{
   @if ((int)TempData["MediaTypeId"] == 1) { 
      @"<audio id='a1' src='#: MediaLocation #' controls='controls' preload='auto' autobuffer><embed height='26' autostart = 'false' type = 'audio/mpeg' width='290' src='#: MediaLocation #'></audio>" +
   }
   else{
     @"&nbsp; #: Description # " +
   }
}
"</td>" +
"</tr></table>"

The above code throws an error: CS1646: Keyword, identifier, or string expected after verbatim specifier: @ at the first instance of the @ sign.

I don't understand why or what this error means. According to the rules of using Razor in MVC4, my syntax should work. Did a little research to be sure and found the syntax to be accurate here But I tried a variation that included a string after the @ as the error suggested and that I figured should work as well:

"<table><tr>" + 
"<td style='color:yellow; width:200px;height:13px;padding-top:0px; margin-top:0px;text-align:center;' class='audiogrid'>" +
@if ((int)TempData["MediaTypeId"] == 1) { 
    @"<audio id='a1' src='#: MediaLocation #' controls='controls' preload='auto' autobuffer><embed height='26' autostart = 'false' type = 'audio/mpeg' width='290' src='#: MediaLocation #'></audio>" +
    }
    else{
          @"&nbsp; #: Description # " +
    }
"</td>" +
"</tr></table>" 

This time I get Compiler Error Message: CS1026: ) expected at the same line that the @ first appears in my code.

I've also tried some other variations but none of them seem to work. What am I doing wrong or missing here? :-(

Basically the control in the cell should change based on the media type.

Upvotes: 0

Views: 1544

Answers (1)

GvS
GvS

Reputation: 52518

Razor is not some sort of string concatenating syntax. You specify the html tags and C# code, and the Razor engine writes them to the client.

Inside an @{ block (or any other code block), you can use C# statements. There's no need for an extra @.

Inside the @if (or any other code block), you can use html tags. No need to prepend them with an @.

Inside the else block, use @: to output html without an html tag.

<table><tr>
<td style='color:yellow; width:200px;height:13px;padding-top:0px; margin-top:0px;text-align:center;' class='audiogrid'>
@{
   if ((int)TempData["MediaTypeId"] == 1) { 
      <audio id='a1' src='#: MediaLocation #' controls='controls' preload='auto' autobuffer><embed height='26' autostart = 'false' type = 'audio/mpeg' width='290' src='#: MediaLocation #'></audio>
   }
   else{
      <text>&nbsp; #: Description # </text>
   }
}

Upvotes: 1

Related Questions