Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » ASP.NET MVC & JQuery » Adding Custom Menu in Html Helper class using Extension Method in ASP.Net MVC

Adding Custom Menu in Html Helper class using Extension Method in ASP.Net MVC

This article will show how to add new functionality in HtmlHelper class using Extension method. This will give step by step explanation of, how to create or add MENU functionality n Html helper class and then use that in view of ASP.Net MVC application.

Author Rank :
Page Views : 5629
Downloads : 126
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
CustomMenu.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Objective:

This article will show how to add new functionality in HtmlHelper class using Extension method. This will give step by step explanation of, how to create or add MENU functionality n Html helper class and then use that in view of ASP.Net MVC application.

Step 1:

Create an ASP.Net MVC application.
 
File->New->Project->web->ASP.Net MVC Application

1.gif

Step 2:

Create Extension method for Html Helper class.

  • Add a class to the project. Give a significant name. I am giving name MenuExtension
  • Convert this class to a static class.
  • This class contains two methods.
  1. Extension method Menu.
     
    • This is extension method to call HtmlHelper.
    • This is taking two parameters as input.
    • First parameter is class name for which this method is extension method.
      (Please see my article on Extension Method for complete detail).

      2.gif
     
  2. Static method GetItems
     
    • This is a static method to the class.
    • This is taking one input parameter
    • This is returning a list
    • This is building the list by encoding the items into a list of anchor tag.

      3.gif

    So, the entire class will look like below listing.

    MenuExtension.cs

    4.gif

    Step 3:

    Create the Controller's Action

    It is a very simple action. It is just creating list of strings and passing that to View. This action is inside controller Home.

    5.gif

    Step 4:

    Create the View
     
    • Right click on Edit action and select Add view
    • Leave the default setting 

      6.gif
       
    • Now in Menu.aspx , import namespace of Extension Method 

      7.gif
       
    • Now , we could see with Html class , .Net intellisense is able to sense the extension method 

      8.gif
       
    • Now add following code in Menu.aspx

      9.gif

    Step 5:

    Adding link at the main page for file uploading
     
    • Open site.master in Views->Shared Folder
    • Add below line in menu tag 

      10.gif
       
    • So, the Menu element will look like

      11.gif

    Step 6:

    Press F5 to run the application

    12.gif

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dhananjay Kumar
Dhananjay Kumar is a developer who blogs at http://debugmode.net/. He is Microsoft MVP ,Telerik MVP and Mindcracker MVP. You can follow him on twitter  @debug_mode
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
MVC Custom Menu by Danny On July 6, 2009

Great post. 

What can be added to this code to open a view for Save Your File
when the Save menu option is clicked ?

public ActionResult Menu()

{

List<String> list = new List<String>();

list.Add("File");

list.Add("Edit");

list.Add("Save");

list.Add("Save As");

list.Add("Exit");

Thanks 

Reply | Email | Modify 
Re: MVC Custom Menu by Dhananjay On July 6, 2009
I did not get your question. Could you please more  explain that
Reply | Email | Modify 
Re: Re: MVC Custom Menu by Danny On July 7, 2009

For example, if I clicked on Save  - it would display a save view in a masterpage.

I noticed that list.add("Save") appears to be an array.
[0]="File", [1]="Save" etc.

If the index is 1, I would call ActionResult Save() which would display /views/save.aspx

Sample code below
------------------------
Html.beginform

To Save your file enter:
 
Textbox: Full path 
Textbox: name of your file
Date Saved :

etc, etc.   


Thanks 
   

Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.