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.
–
–
–
–
alertClass = "panel-danger";
<div id="panel_@y" class="panel @alertClass">Some Text_@y</div>
–
–
–
–
–
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>
–
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.