Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

20 Experts available now in Live!

Get 1:1 Help Now