ASP.NET MVC Razor Won't Accept My Valid Markup

asp.net, asp.net-mvc, razor

Solution

For reasons I don't understand, the following corrected the issue:

@if (Model.FeaturedDestinations != null && Model.FeaturedDestinations.Count() > 0)
{
    int column = 0;

    foreach (var d in Model.FeaturedDestinations)
    {
        column++;

        if (column > 4)
        {
            @:</div>
            column = 1;
        }

        if (column == 1)
        {
            @:<div class="row-fluid">
        }
        <div class="span3">
            @RenderDestination(d)
        </div>
    }
    @:</div>
}

Note the addition of `@:` before several tags. I don't know why these are necessary--the Razor highlighting indicated that it recognized these were tags and not code.

Also, why did this make the error go away? The thing that caused the run-time error has not changed. Perhaps someone can fill in the blanks for me.

Problem

I like the Razor syntax a lot, but it certainly falls short of perfect. For example, I have the following block of markup. ``` @if (Model.FeaturedDestinations != null && Model.FeaturedDestinations.Count() > 0) { int column = 0; foreach (var d in Model.FeaturedDestinations) { column++; if (column > 4) { </div> @{ column = 1; } } if (column == 1) { @:<div class="row-fluid"> } <div class="span3"> @RenderDestination(d) </div> } </div> } ``` So, the editor gives me the squiggly lines indicating that I have an ending `<div>` tag before an opening one. I can live with that. But when I run the app, I actually get the following run-time error: Encountered end tag "div" with no matching start tag. Are your start/end tags properly balanced? Obviously, I cannot live with that! So is there any trick for dealing with this case? I know what I'm doing as far as the markup I want. But Razor doesn't think so and it's taking over. And why the heck is MVC wasting cycles checking for balanced tags?

Original source