1

I have 2 Objects, User and Menu, I want to loop into User.Menu to creat links like this:

@for (int i = 0; i < _Usuario.Menu.Count(); i++)
{
    @Html.ActionLink( Convert.ToString(_Usuario.Menu.LinkName), Convert.ToString(_Usuario.Menu.ActionName),                    Convert.ToString(_Usuario.Menu.ControllerName))
}

But i dont have a counter for User.Menu, how could this be done ?

public class User
{
    public Int64 Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public DateTime LoginTime { get; set; }
    public Menu Menu { get; set; }
    public List<string> Objects { get; set; }
    public List<string> Controllers { get; set; }
    //public List<string> Roles { get; set; }

    public User()
    {
        Objects = new List<string>();
        Controllers = new List<string>();
    }
}

public class Menu
{
    public List<string> LinkName { get; set; }
    public List<string> ActionName { get; set; }
    public List<string> ControllerName { get; set; }

    public Menu()
    {
        LinkName = new List<string>();
        ActionName = new List<string>();
        ControllerName = new List<string>();
    }
}
0

2 Answers 2

1

Your menu class doesn't make much sense as it implies that the link, action, and controller names are three separate sets of items. In reality there is a single set of menu items each consisting of a link, action, and controller. So this means you can rewrite Menu as:

public class Menu
{
    public List<MenuItem> Items { get; set; }

    public Menu()
    {
        Items = new List<MenuItem>();
    }
}

public class MenuItem
{
    public string LinkName { get; set; }
    public string ActionName { get; set; }
    public string ControllerName { get; set; }
}

You'll have to rewrite your code that sets up menu, but that should be easy enough. Then it is easy to loop through in your view.

@for (int i = 0; i < _Usuario.Menu.Items.Count(); i++)
{
    @Html.ActionLink(_Usuario.Menu.Items[i].LinkName, _Usuario.Menu.Items[i].ActionName, _Usuario.Menu.Items[i].ControllerName)
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would prefer to see a foreach rather than this way, but +1 anyway.
0

An alternative way is to create a display template for the model and use @Html.DisplayFor(). This way you do not need to worry about the loop as it will do it for you. This is a good way to keep your razor view nice and clean.

Example

public class MenuItem
{
    public string LinkName { get; set; }
    public string ActionName { get; set; }
    public string ControllerName { get; set; }
}

Display template (menuitem.cshtml):

@model MenuItem

@Html.ActionLink(Model.LinkName, Model.ActionName, Model.ControllerName)

View:

@model IEnumerable<MenuItem>
@Html.DisplayForModel()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.