0

I have a JSON file where I am using a foreach loop to loop through that file. Everything is working just fine. At the moment everything is printed in the below code.

@{ bool anyRoomsForChildrenUnder4 = false; foreach (var room in Model.Order.OrderLines) { anyRoomsForChildrenUnder4 = room.NumberChildren0to3 > 0; if (anyRoomsForChildrenUnder4) { break; } } } <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body> @{bool prcb = !string.IsNullOrWhiteSpace((string)Model.Order.Store.JsonDynamic.Da.HotelData.PaymentRequiredForChildrenBreakfast); } <tr> <th class="small-12 large-6 columns first"> <table> <tr> <th width="300"> <p class="text-left small-text-left"> @if (prcb) { <strong>Lunch for children under 3</strong> } </p> </th> </tr> </table> </th> <th class="small-12 large-6 columns last"> <table> <tr> <th width="300"> <p class="text-left small-text-left"> @if (prcb) { <span>@Model.Order.Store.JsonDynamic.Da.HotelData.PaymentRequiredForChildrenBreakfast</span> } </p> </th> <th class="expander"></th> </tr> </table> </th> </tr> <!-- Price for children breakfast--> @{bool pfcf = !string.IsNullOrWhiteSpace((string)Model.Order.Store.JsonDynamic.Da.HotelData.PriceForChildrenBreakfast); } <tr> <th class="small-12 large-6 columns first"> <table> <tr> <th width="300"> <p class="text-left small-text-left"> @if (pfcf) { <strong>OA dummy text for now</strong> } </p> </th> </tr> </table> </th> <th class="small-12 large-6 columns last"> <table> <tr> <th width="300"> <p class="text-left small-text-left"> @if (pfcf) { <span>@Model.Order.Store.JsonDynamic.Da.HotelData.PriceForChildrenBreakfast</span> } </p> </th> <th class="expander"></th> </tr> </table> </th> </tr> </body> </html> 

But The below code only needs to get printed if the (anyRoomsForChildrenUnder4) is true. If it is not true, the below code should not execute.

(anyRoomsForChildrenUnder4) > 0 : true -> Print

(anyRoomsForChildrenUnder4) = 0 : false -> Do not print

Does anybody knows how I can do that?

1
  • It sounds like an if statement would do exactly what you want. Commented Feb 16, 2017 at 18:01

1 Answer 1

2

Approach 1

Just store the flag in the ViewBag like this:

@{ bool anyRoomsForChildrenUnder4 = false; foreach (var room in Model.Order.OrderLines) { anyRoomsForChildrenUnder4 = room.NumberChildren0to3 > 0; if (anyRoomsForChildrenUnder4) { ViewBag.AnyRoomsForChildrenUnder4 = true; } } } 

Then you can access it wherever you want like this:

@if(ViewBag.AnyRoomsForChildrenUnder4) { // ... print whatever you want } 

Approach 2

But it is not a good idea to have all that C# code in your view. Do all of that in your controller and pass a model to your view. This model will have everything your view needs and it can be simple. A better approach is to pass a model to your view from the controller. Have a model like this:

public class SomeModel // Give this a better name { public bool AnyRoomsForChildrenUnder4 { get; set; } // Put other properties which your view needs } 

Then in your controller do this (keep in mind I am making many assumptions here so take this as an example to follow):

var model = new SomeModel(); bool anyRoomsForChildrenUnder4 = false; foreach (var room in Model.Order.OrderLines) { anyRoomsForChildrenUnder4 = room.NumberChildren0to3 > 0; if (anyRoomsForChildrenUnder4) { model.AnyRoomsForChildrenUnder4 = true; break; } } return View(model); 

Then in your view:

@model SomeModel // ... code @if (Model.AnyRoomsForChildrenUnder4) { // Your code here } 
Sign up to request clarification or add additional context in comments.

7 Comments

Hello. I have just updated my question, because I feel that I was a little bit unclear. THe foreach loop is outside the <html> tag. Therefore I cannot set the if statement inside the foreach loop.
@Mimi See my edit. However, you should do what I suggested using the model approach so minimize all the C# code you have in your view.
Thank you for the answer. The problem for me is that the code is for an email confirmation for a hotel. I cannot models etc. I am forced to have all my code in one document. That is the big problem for me at the moment. So the code in my question, I cannot change it around and so on. I have to make it work from how the code is there.
It problaly would. But it is not possible for me to do it like you do. Long story.. The code it for emails, and we are running the code through a marketing automation system we have. Therefore I have to have all my code, html, c#, css, razor etc in the same document. I have to find a solution where I can print the tables without changing around in the code in my question. But seems like a quite hard task.
What do you mean? I don't think you get my answer. Just use the first approach
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.