Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS Start 2 37
ASP.NET Built-In Report Creator / Viewer 5 17
VB.net/VSTO Excel Add-in 2 14
Convert VB web project to C# 3 27
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

680 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