1
@if (@Model.Persons.Count > 0)

    var data= @Model.Persons;
}
else
{
    <b>no data!</b>
}

when I add foreach statement inside if statement else block of code become broken

@if (@Model.Persons.Count > 0)
{        
    var data= @Model.Persons;
    foreach(var item in @data){
       ...
    }
}
else{ // this now becomes broken after adding foreach 
}
2
  • What happens if you remove the @ from data in the foreach line? Commented May 28, 2015 at 11:40
  • I think that when you're using a @if or a tag like a @foreach (these kind of helpers), when you got a block with brackets just below it, you're in c# code, so the @ befor Model and especially here, befor data, is useless. Try it ! Commented May 28, 2015 at 11:46

1 Answer 1

7

Remove the @ from Model.Persons.Count, from Model.Persons and from data. You already created a block using the @if (){ }.

@if (Model.Persons.Count > 0)
{        
    var data= Model.Persons;
    foreach(var item in data){
      ...
    }
}
else
{
}
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.