MENU CONTROLS IN MVC

ravi kumar

Total Post:9

Points:63
Posted by  ravi kumar
 2730  View(s)
Ratings:
Rate this:
  Hi I am new to mvc. how to create a dynamic menu controls in mvc please help me
  1. Chris Anderson

    Post:65

    Points:455
    Re: menu controls in mvc

    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
  1. ravi kumar

    Post:9

    Points:63
    Re: menu controls in mvc

    Thank you Rohit

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!