Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am creating a partial view thats rendered ul and li
dynamically but output will be display wrong i need below output.

sale
 Add sale
 View Sale
 Delete Sale
Product
 Add Product
 View Product


C#
@model IEnumerable<partialpageexample.models.permission>

@if (Model.Any())
{
    var result = TreeView(Model);
    @Html.Raw(result)
}

@functions
    {
    public string TreeView(IEnumerable<partialpageexample.models.permission> Model)
    {
        string Htmlstring = string.Empty;

        if (Model.Count() > 0)
        {
            Htmlstring += "<ul>";
            foreach (var parentNode in Model)
            {
                
                Htmlstring += "<li>" + parentNode.DisplayName;

                Htmlstring += TreeView(Model.Where(x => x.ParentId.Equals(parentNode.PermisionId)));

                Htmlstring += "</li>";

            }
            Htmlstring += "</ul>";

        }
        return Htmlstring;

    }

}
Posted
Updated 11-May-16 0:08am
v2

1 solution

That's completely the wrong way of doing MVC. Your controller passes data to the view and it is up to the view to decide how that data is rendered, you don't generate html in your controller as that completely breaks the paradigm.

So create a data model that represents your data (like a list of child\parent objects) and use the view to represent them as html.

Model
C#
public class MenuModel
{
    public string Name { get; set; }
    public List<MenuModel> Children { get; set; }
}


Controller
C#
public ActionResult Index()
{
    List<MenuModel> model = new List<MenuModel>();

    model.Add(new MenuModel { Name = "Sale", Children = new List<MenuModel> {
        new MenuModel { Name = "Add sale" },
        new MenuModel { Name = "View sale" },
        new MenuModel { Name = "Delete sale" } } });
            
    model.Add(new MenuModel { Name = "Product", Children = new List<MenuModel> {
        new MenuModel { Name = "Add product" },
        new MenuModel { Name = "View product" } } });

    return View(model);
}


View
Razor
@model List<MenuModel>

<ul>
    @foreach (var menuItem in Model)
    {
        <li>@menuItem.Name</li>
        if(menuItem.Children != null && menuItem.Children.Any())
        {
            <ul>
                @foreach (var childItem in menuItem.Children)
                {
                    <li>@childItem.Name</li>
                }
            </ul>
        }
    }
</ul>
 
Share this answer
 
Comments
awaisshabir 11-May-16 7:58am    
i need recursion function

model.Add(new MenuModel { Name = "Sale", Children = new List<menumodel> {
new MenuModel { Name = "Add sale" },
new MenuModel { Name = "View sale" },
new MenuModel { Name = "Delete sale",Children = new List<menumodel> {
new MenuModel { Name = "Add sale" } } } });
F-ES Sitecore 11-May-16 9:15am    
The data already supports recursion, you just need to output the child <ul> sections via a function that is called recursively.
awaisshabir 12-May-16 2:27am    
my database structure like

menuID MenuName ParentID
1 sale 0
2 Add Sale 1
3 Product 0
4 WareHouse 3
5 Laptop 4
6 Hp 5
7 Toshiba 5
Karthik_Mahalingam 11-May-16 9:33am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900