Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am currently having difficulty successfulling adding the following code to my View, which is a .cshtml file.

I have an if and else statement that render different panels depending on a condition.

My code is as follows:

foreach (var item in groupItem)
   @if (item.NextDue < DateTime.Today)
     <div class="panel panel-danger" id="panel_@i">
     <div class="panel panel-info" id="panel_@i">

I have tried lots of combinations of @{ around the code, but I think that the issue is the ids also have an @ symbol. If I comment out the code within the foreach loop, the code executes fine. However, adding the code into the foreach loop results in an error of "closing } of the foreach loop is not found"

Any help on being able to execute this code would be greatly appreciated.

What is True in the if block (have you declared a boolean variable named True?) And what is i - is this in a loop? – user3559349 Aug 9, 2015 at 1:36 True is the result of a Boolean that I have declared. The actual code is @if(overdue == True), but thought this was irrelevant for the main question. Yes this code is in a loop, foreach (item) and i increases after every execution of this code. – fuzzi Aug 9, 2015 at 1:39 It would just need to be @if(overdue) {..} :) But post your real code including the loop. No point posting code which cant be tested and wont even compile – user3559349 Aug 9, 2015 at 1:47 Your already inside a razor block (the foreach loop) so you don't need the @if (.. - it should be just if(... Can you post the exact error message you getting – user3559349 Aug 9, 2015 at 2:01 alertClass = "panel-danger"; <div id="panel_@y" class="panel @alertClass">Some Text_@y</div> The use of the @i is because the panels are appearing for a large data set, and the panels need to relate to unique ids. My goal is to render either red or blue panels based on a separate condition. Therefore, in this case, the unique id's would be reused as this code relies in a foreach loop. – fuzzi Aug 9, 2015 at 1:22 so...are you asking how to get unique ids out of a foreach loop? Post the loop...if your looping over a data set then I would use the primary key of the record for the panel id. – BillRuhl Aug 9, 2015 at 1:26 Currently, this snippet of code is not working in my View, i.e. the if closing { and else statement are not being read as C#. I want to know how I can make them read as C#. The error is "the block is missing a closing "}" character". How can I get rid of this error and execute this code? – fuzzi Aug 9, 2015 at 1:29 You have a syntax error in your page. Check the nesting of your code blocks. That error says it all...your missing a } some where or you have an extra { somewhere. Post the view. – BillRuhl Aug 9, 2015 at 1:32 I know, but if I comment out just the snippet of code I posted, the View renders perfectly and there is no error. – fuzzi Aug 9, 2015 at 1:34

The problem is that you have a starting <div> tag but no ending tag, so razor doesn't expect the ending bracket there. You can use the <text> tag to specify what the content is when razor has trouble parsing it:

@foreach (var item in groupItem)
   if (item.NextDue < DateTime.Today)
     <text><div class="panel panel-danger" id="panel_@i"></text>
     <text><div class="panel panel-info" id="panel_@i"></text>
                Thanks, however, the issue is more with the recognition of the else statement and the closing parenthesis of the if statement.  Therefore, this does not fix the issue.
– fuzzi
                Aug 9, 2015 at 1:15

If all you need is to change the panel color base on a condition, you can do this:

<div class="panel @(item.NextDue < DateTime.Today ? "panel-danger" : "panel-info")">

If the condition is true, if will use panel-danger. otherwise it will use panel-info.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.