This blog has, IMO, some great resources. Unfortunately, some of those resources are becoming less relevant. I'm still blogging, learning tech and helping others...please find me at my new home on http://www.jameschambers.com/.

Thursday, March 17, 2011

ASP.NET MVC3: The Razor View Engine and Text

I knew this, but I had a brain fart here and couldn’t figure out what was wrong. I was trying to output some simple text to the browser based on some condition:

image

I started to get the following error message:

The type or namespace name 'Do' could not be found (are you missing a using directive or an assembly reference?)

Along with a list of others:

image

But nothing really said what was wrong.

Nothing’s really wrong, per se, but Razor can’t figure out what I want to do here and doesn’t know that Do should be text and not me trying to code something. To fix it, simply wrap the text in a <span> tag and roll:

image

Tada! </facepalm>

The only reason I ran into this was because I copy & pasted the text. Normally you’d get IntelliSense as soon as you start typing and that should throw up a context flag pretty quick.

4 comments:

  1. or wrap the text with

    ReplyDelete
  2. You can optionally wrap nested content with a block for cases where you have content that you want to render to the client without a wrapping tag:

    @if(true)
    {

    Do something @DateTime.Now

    }

    ReplyDelete
  3. Hi Henrik...I think the Blogger engine filtered out some of your text. Were you meaning to use an HTML-style text tag? The text tag is not rendered. Alternatively, you can use a @: as a line prefix to write out a line if you don't want to output the html. Cheers.

    ReplyDelete
  4. Hi ChipOne

    Correct the < text > tag is missing :)
    @if(true)
    {
    < text >
    Do something @DateTime.Now
    < / text >
    }

    ReplyDelete