Solved

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

Posted on 2012-03-20
4
460 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
  • 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 300 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SlingPlayer alternatives 1 35
Error in query expression 3 37
Server Error 11 47
Not seen Link button 5 17
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now