Umar Khan
Umar Khan

Reputation: 454

razor html output result

I am trying to output the results in my @helpers code and the code looks like this

@helpers listfiles(String ID, String CNumber,){
   foreach(Loopitem I in GetLoop("items")){
      if(I.GetValue("userId") == ID){
            <li>@I.GetValue("name")</li>
         }else{
              If(I.GetValue("userId") != ID){
                 <li>@I.GetValue("name")</li>
                }
            }
    }    

} 

As a result I get all li elements but what I want is that if the statement is true it should wrap all the li elements in ul element and for the else statement it should wrap all the li in new UL element. Please help

Upvotes: 0

Views: 52

Answers (2)

har07
har07

Reputation: 89285

One possible way by using two foreach, one for each user ID group :

@helpers listfiles(String ID, String CNumber,){
    <ul>
    foreach(Loopitem I in GetLoop("items").Where(o => o.GetValue("userId") == ID)){
        <li>@I.GetValue("name")</li>
    }
    </ul>
    <ul>
    foreach(Loopitem I in GetLoop("items").Where(o => o.GetValue("userId") != ID)){
        <li>@I.GetValue("name")</li>
    }
    </ul>
} 

Upvotes: 1

Mohamed Hamdy
Mohamed Hamdy

Reputation: 11

You mean something like this:

@helpers listfiles(String ID, String CNumber,){
   var lstTrue = new List<>();
   var lstFalse = new List<>();
   foreach(Loopitem I in GetLoop("items")){
      if(I.GetValue("userId") == ID)
            lstTrue.Add(I);
      else
            lstFalse.Add(I);
    }
   if(lstTrue.Count()>0)
   {
      <ul> foreach(var I in lstTrue){<li>@I.GetValue("name")</li>}</ul>
   }
   if(lstFalse.Count()>0)
   {    
      <ul> foreach(var I in lstTrue){<li>@I.GetValue("name")</li>}</ul>
   }   
} 

Or you can make use of Lambda expression to reduce lines of code.

Upvotes: 0

Related Questions