Home > DeveloperSection > Forums > menu controls in mvc
ravi kumar

Total Post:9

Points:63
Posted on    November-01-2011 3:19 AM

 ASP.NET MVC ASP.NET MVC 
Ratings:


 2 Reply(s)
 2370  View(s)
Rate this:
  Hi I am new to mvc. how to create a dynamic menu controls in mvc please help me


Chris Anderson
Chris Anderson

Total Post:65

Points:455
Posted on    November-01-2011 7:12 AM

Hi,

You can dynamically create menu control in asp.net mvc by using ViewBag or ViewData properties.
For eg:
Create a action method (Index) in a controller as given below:

public ActionResult Index()
        {
            List<string> hobby = new List<string>();
            hobby.Add("Cricket");
            hobby.Add("Football");
            hobby.Add("Tennis");

            ViewBag.Hobbies = hobby; //hobby is List
            return View();
        }

In an Index View use the below code:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<html>
<head runat="server">
    <title>Index</title>
    <style type="text/css">
        .required
        {
            text-decoration:none;
        }
        li
        {
            list-style-type:none;
            display:inline;
            margin-left:10px;
        }
    </style>
</head>
<body>
    <div>
        <ul>
            <% foreach (var hobby in ViewBag.Hobbies) { %>
            <li>
                <font color="blue"><%: Html.ActionLink((string)hobby, "Index", "Home", new { @class = "required" })%></font>
            </li>
            <% } %>
        </ul>
    </div>
</body>
</html>

Modified On Nov-01-2011 07:17:08 AM

ravi kumar

Total Post:9

Points:63
Posted on    November-01-2011 11:18 PM

Thank you Rohit

Don't want to miss updates? Please click the below button!

Follow MindStick