Solved

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

Posted on 2012-03-20
4
472 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

831 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