简体   繁体   English

剃刀html输出结果

[英]razor html output result

I am trying to output the results in my @helpers code and the code looks like this 我正在尝试在@helpers代码中输出结果,并且代码看起来像这样

@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. 结果,我得到了所有li元素,但我想要的是,如果该语句为true,则应将所有li元素包装在ul元素中,对于else语句,应将所有li包装在新的UL元素中。 Please help 请帮忙

One possible way by using two foreach , one for each user ID group : 一种可能的方法是使用两个foreach ,每个用户ID组一个:

@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>
} 

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. 或者,您可以使用Lambda表达式来减少代码行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM