Search code examples
htmlrazorasp.net-mvc-5html-helper

how to add space for child item in dropdown?


I'm trying to show the parent child elements in dropdown select list.as the following imageenter image description here

i have the code as in 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)
        }

    }
}

but it showed as enter image description here

How can i display as the first picture.


Solution

  • I Got Answer.

    Adding another field in model class as Hierarchy.

    Adding space using the hierarchy. I add my code for refer.

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