0

I have code like this in my razor index view:

@for (decimal hour=7m; hour<=20.5m; hour +=0.5m)
{
  var item = @Model.Items.FirstOrDefault( i=> i.Hour == hour);
  if (item != null)
  {
    <td colspan="@item.TimeBlocks">
      @item.Description
    </td>
    hour += (item.TimeBlocks-1)* 0.5m;
  }
  else
  {
    <td>
      <a href="/Items/Add/@Model.UserId">+</a>
    </td>
  }
}

For some reason, the VS2013 consistently tells me "} expected" in the Error List window. The syntax highlighter shows everything highlighted fine until the curly brace right before the else.

I have tried using @: for the html tags. It works fine in the if block, but in the else block I get the following error: ":" is not valid at the start of a code block.

I have tried wrapping the html tags in a tag as well, but that doesn't work either.

How would I do this to get it to render?

4
  • 1
    Are you sure your syntax for a foreach loop is correct? Commented Jan 2, 2015 at 1:37
  • @rexcfnghk - Typo when creating the question. My code in VS is just for, not foreach Commented Jan 2, 2015 at 1:38
  • 2
    var item = @Model.Items.FirstOrDefault( i=> i.Hour == hour); this line shouldn't have the @ symbol as you aren't outputting the result to the response stream Commented Jan 2, 2015 at 8:37
  • @Slicksim That was the problem! You want to add that as an asnwer and I will accept it? Commented Jan 2, 2015 at 16:59

1 Answer 1

1

You have a mistake in your code.

The following line

var item = @Model.Items.FirstOrDefault( i=> i.Hour == hour);

Should actually read:

var item = Model.Items.FirstOrDefault( i=> i.Hour == hour);

Removing that will fix your code

Sign up to request clarification or add additional context in comments.

Comments

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.