Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mvc helper extension - need to access and modify a named element in the view

Posted on 2012-03-20
4
Medium Priority
?
496 Views
Last Modified: 2012-08-14
I've written an mvc helper extension for rendering a site map menu.  I can put the menu in the top menu location of the at the point of the  html helper in the view, but I also need to render some left nav menu structures in another location (left side) of the screen... so how can I render html inside the helper, to a second location (like a div) that is not the place in the view where I place the <% htmlhelper.renderMenu %>  

I need the left nav to be in sync with the selected top menu node... so I need to generate the top menu, and based on the selected node in the menu, generate a left nav menu in a second element / div..
The site map has the top menu and left nav information, but I need to know how I can acccess and render to the left nav div?

So  <% htmlhelper.renderMenu %>  is in top menu div, and my render populates it fine...
but I have a left nav div, and I need to generate a left nav div content at the same time, in the same helper extension... I don't know how to access this left nav div so I can render content to it...
0
Comment
Question by:jparlato
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37745697
here is something to help you from
http://stackoverflow.com/questions/879852/asp-net-mvc-display-a-view-from-another-controller


By default, ASP.Net MVC checks first in \Views\[Controller_Dir]\, but after that, if it doesn't find the view, it checks in \Views\Shared.

The shared directory is there specifically to share Views across multiple controllers. Just add your View to the Shared subdirectory and you're good to go.

Read These As Well:-
http://stackoverflow.com/questions/1673419/building-pages-with-multiple-views
http://stackoverflow.com/questions/4721521/multiple-view-in-same-page-asp-net-mvc2
http://stackoverflow.com/questions/3348163/asp-net-mvc-loading-multiple-partial-views-into-a-single-div-using-jquery
0
 

Author Comment

by:jparlato
ID: 37746107
I'm not trying to build mulitple partial views... I need to render html to both the location in the view, where I have my htmlhelper.rendermenu and to a second div - from the same html helper.  The htmlhelper know how to render html to the location it is called from; but how can it address a second div on the view... and render some html there as well.
0
 

Author Comment

by:jparlato
ID: 37746128
public static string Label(this HtmlHelper helper, string target, string text)      
    {            

  return String.Format("<label for='{0}'>{1}</label>", target, text);      

    }

As in this example, you see it can render to the target.... but I need to render to the target, and to a second target (the left div) .  How can I get the htmlhelper to have two targets?
I guess that it may not be possible, since it wants to return a single result?
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 1200 total points
ID: 37746168
Try This:-
public static Helper
{
public static MvcHtmlString GetMenu(this HtmlHelper)
{
  //Get List from Db and append it to a StringBuilder object.
  StringBuilder sbMenu=new StringBuilder();
  sbMenu.Append("<Div>"); //Append your menu code here
  return new MctHtmlString(sbMenu.ToString());
}
public  static MvcHtmlString GetLeftCategoriesList(this HtmlHelper,int MenuID)
{
  //Get List from Db and append it to a StringBuilder object.
  StringBuilder sbChildMenu=new StringBuilder();
  sbChildMenu.Append("<Div>"); //Append your child menu code here
  return new MctHtmlString(sbChildMenu.ToString());
}
}

Open in new window


In View use GetMenu extension to display the menu.
and then in the Left Portion of your page  Call the GetLeftCategoriesList helper method to get the categoryList information.

By Default HTMLExtension Method returns HTML string,so no need to convert it to string.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you will learn how to create a free basic website on Bitbucket, a git service provider. Polymer creates dynamic HTML components, which allow more flexibility than static HTML. This tutorial uses Ubuntu Linux but can also be done on W…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question