Вы можете сделать это следующим образом
public class InfoClass
{
public string PartyName { get; set; }
public string SubPartyName { get; set; }
}
public class NewInfoClass
{
public string PartyName { get; set; }
public List<string> SubPartyName { get; set; }
}
Действие контроллера
public ActionResult Index()
{
List<InfoClass> infos = new List<InfoClass>();
infos.Add(new InfoClass() { PartyName = "Party A", SubPartyName = "SubParty A" });
infos.Add(new InfoClass() { PartyName = "Party A", SubPartyName = "SubParty B" });
infos.Add(new InfoClass() { PartyName = "Party A", SubPartyName = "SubParty B" });
infos.Add(new InfoClass() { PartyName = "Party A" });
infos.Add(new InfoClass() { PartyName = "Party B" });
infos.Add(new InfoClass() { PartyName = "Party C" });
var results = from p in infos
group p.SubPartyName by p.PartyName into g
select new { PartyName = g.Key, SubPartyNames = g.ToList().Distinct() };
List<NewInfoClass> newList = new List<NewInfoClass>();
int count = 0;
foreach (var item in results)
{
newList.Add(new NewInfoClass()
{
PartyName = item.PartyName,
});
newList[count].SubPartyName = new List<string>();
foreach (var item2 in item.SubPartyNames)
{
if (item2 != null)
{
newList[count].SubPartyName.Add(item2);
}
}
count++;
}
return View(newList);
}
Просмотр
@model List<DemoApplication.Models.NewInfoClass>
<ul id="list">
@foreach (var item in Model)
{
<li data-group="@item.PartyName" data-expanded="true">
@item.PartyName
@foreach (var item2 in item.SubPartyName)
{
<ul>
<li data-group="@item2" data-expanded="true">@item2</li>
</ul>
}
</li>
}