Q:

How can I display from a script block with Razor?

I'm trying to learn Razor/MVC3 and having a bit of trouble with this partial view DisplayTemplate. 
@{
    DateTime? date = ViewData.Model;

    if(date.HasValue) {
        date.Value.ToShortDateString();
    }
}

This is what I'm trying to do. Obviously I'm not doing it correctly, because it's not working. I've tried it several different ways I could think of and only received random errors. This method simply doesn't return anything ever. 

A:

every where inside code block to force switching to text mode use <text></text> snippet.
@{
    DateTime? date = ViewData.Model;
    string displayDate = "";

    if(date.HasValue) {
         <text>
               @date.Value.ToShortDateString()
         </text>
    }
}

cheers!

A:

You're not rendering anything to the page with this syntax; all that code does is declare a variable date and set it to a value in the model.  Try something like this ...
@{
    DateTime? date = ViewData.Model;
    string displayDate = "";

    if(date.HasValue) {
        displayDate = date.Value.ToShortDateString();
    }
}

@displayDate

Or even simpler, just this:
@if (ViewData.Model.HasValue) 
{
    <span>@ViewData.Model.ToShortDateString()</span>
}

