foreac(abc.xyz entry in tlist)
{
@{
List<abc.Models.dian> dianobjlist = abc.Service.Class1.dianlist();
}
@foreach (abc.Models.dian dianobj in dianobjlist)
{
<div class="dian">
@if (dianobj.dian1 == entry.dian1)
{
<input type="checkbox" value='@dianobj.dian1' name="dianl" checked="checked"/>
}
else if (dianobj.dian1 == entry.dian2)
{
<input type="checkbox" value='@dianobj.dian1' name="dianl" checked="checked"/>
}
else if (dianobj.dian1 == entry.dian3)
{
<input type="checkbox" value='@dianobj.dian1' name="dianl" checked="checked"/>
}
else if (dianobj.dian1 == entry.dian4)
{
<input type="checkbox" value='@dianobj.dian1' name="dianl" checked="checked"/>
}
else
{
<input type="checkbox" value='@dianobj.dian1' name="dianl"/>
}
<label>@dianobj.dian1</label>
</div>
}
}
这是我编写的代码,用于检查所有那些值满足if条件的复选框。现在,我想要的是,所有被检查的东西应该在顶部。我们的列表中有50多个dian,但根据编码可以检查最多4个。所有检查过的项目都应显示在顶部,所有未检查的项目应显示在
下方答案 0 :(得分:0)
您可以根据列表是否符合您的标准来订购列表(因此会进行检查)。
因此您可以使用以下内容替换第二个foreach
:
@foreach (abc.Models.dian dianobj in dianobjlist.OrderBy(d =>
d.dian1 == entry.dian1 ||
d.dian1 == entry.dian2 ||
d.dian1 == entry.dian3 ||
d.dian1 == entry.dian4))
{
// print out your inputs
}
理想情况下,我认为您会想要将决定是否检查它的逻辑移动到您的类中:
public class dian
{
public bool IsCheckBoxChecked(abc.xyz entry)
{
return
this.dian1 == entry.dian1 ||
this.dian1 == entry.dian2 ||
this.dian1 == entry.dian3 ||
this.dian1 == entry.dian4;
}
}
然后你可以简化你的观点:
@foreach(abc.xyz entry in tlist)
{
List<abc.Models.dian> dianobjlist = abs.Service.Class1.dianlist();
foreach (abc.Models.dian dianobj in dianobjlist.OrderByDescending(d => d.IsCheckBoxChecked(entry)))
{
<div class="dian">
@if(dianobj.IsCheckBoxChecked(entry))
{
<input type="checkbox" value='@dianobj.dian1' name="dianl" checked="checked"/>
}
else
{
<input type="checkbox" value='@dianobj.dian1' name="dianl"/>
}
<label>@dianobj.dian1</label>
</div>
}
}