2015-07-10 88 views
2

我试图以显示下拉列表中父子元素选择list.as以下图像enter image description here如何在下拉菜单中添加子项目的空间?

我的代码中的foreach

@helper GetOption(List<Project_Cost_Management_System.Models.ChartOfAccount> model1, Guid? parentid) 
{ 
    foreach (var item3 in model1.Where(s => s.ParentAccountId.Equals(parentid))) 
    { 


     var zs3 = model1.Where(s => s.ParentAccountId == item3.Id); 
     int count3 = zs3.Count(); 
     if (count3 >= 0) 
     { 
      if(@item3.ParentAccountId == null) 
      { 
      <option value="@item3.Id">@item3.Name</option> 
      } 
      else 
      { 
       var cout = model1.Count(); 
       <option value="@item3.Id"> @item3.Name @model1.Where(s => s.dataid == @item3.dataparentid).Select(d => d.Name).First(). </option> 
      } 
     } 
     if (count3 > 0) 
     { 
      @GetOption(model1, item3.Id) 
     } 

    } 
} 

,但它表现为enter image description here

如何我可以显示为第一张照片吗?

+0

您可以发布当前输出演示吗? –

+0

秒图是 –

+0

我的意思是现场演示。我们如何分析代码? :)你有一个动态代码。 –

回答

0

我得到了答案。

添加模型类的层次另一个领域。

使用层次添加空间。我添加了我的代码以供参考。

@helper GetOption(List<Project_Cost_Management_System.Models.ChartOfAccount> model1, Guid? parentid) 
{ 

    foreach (var item3 in model1.Where(s => s.ParentAccountId.Equals(parentid))) 
    { 

     var zs3 = model1.Where(s => s.ParentAccountId == item3.Id); 
     int count3 = zs3.Count(); 
     if (count3 >= 0) 
     { 
      if (@item3.ParentAccountId == null) 
      { 
       <option value="@item3.Id">@item3.Name</option> 
      } 
      else 
      { 
       int str = @item3.Hierarchy * 3; 
       string str1 = " ".ToString().PadRight(str); 
       str1 = str1.Replace(" ", "\u00a0"); 
       <option value="@item3.Id">@str1 @item3.Name</option> 

      } 
     } 
     if (count3 > 0) 
     { 
      @GetOption(model1, item3.Id) 
     } 

    } 
} 
1

您可以排序的实现你在找什么用<optgroup>,所以你呈现的HTML将结束是这样的:

<option value="...">Professional Fees</option> 
<optgroup label="Property Related Expenses"> 
    <option value="...">Maintenance Contribution</option> 
    ... 
</optgroup> 
... 

你可能有这样做的唯一的问题是,您的实际分组本身并不是有效的选项,即你不能选择“属性相关的费用”,因为它只是一个分组标签。您也无法以这种方式真正控制右对齐的描述性文字。一般来说,HTML select元素相当严格,并且不允许进行大量的自定义。

当你需要更先进的功能,则必须将某种库的创建模仿一个选择列表中用更多的可定制的HTML元素的功能的“控制”。 interwebs上有一百万个和一个不同的类库,但我特别喜欢Select2.js。特别是针对您的情况,请参阅“模板”部分。

+1

如果我使用opgrop意味着我如何使选项组被选中的值,因为父母也是一些孩子。 –

+0

你不行。这才是重点。我只是指出,optgroup是最好的,你可以用直接的HTML做,这是不够的。你将不得不为此使用某种JS解决方案。 –

相关问题