0

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.

1 Answer 1

1

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>
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the enlightenment GyS. The @ inside the code blocks were my futile attempt in effort of trying to determine what the error meant. I modified my code based on your recommendation and no longer receive either of the other errors but am now getting "Parser Error Message: Inline markup blocks (@<p>Content</p>) cannot be nested. Only one level of inline markup is allowed." in the else block using @: Any ideas what that means? I understand your explanation of its use but the error doesn't make sense to me
Take a look here: aspnetwiki.com/… Looks like your are nesting tags like <text>.
If I am its not immediately obvious. But I'll definitely try the alternative approach provided in your link. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.