Creating a navigation for entire website - asp.net-mvc

HI I am just learning asp.net mvc 3 and I am tryng to create a common menu for my entire application.I understand I can do that in _Layout.cshtml file witch is the default template for every page.
Now I have looked into sections for adding a template insite _Layout.cshtml but from what I can gather I need to define the section in every view.
I already have the logic for accesing the data defined in a separate class.All I need is to call the method witch will return a Dictionary<string , List<string>> , and then display the data by looping into it.
Aldo I could probably do this directly inside the Index.cshtml file by using the razor syntax I believe there must be a better way
So is there a way to create a template that can then bee added inside the _Layout.cshtml?

Creating a section for the menu on each view could work but I think another approach could be easier to mantain. On your layout itself use #Html.Partial to render your menu. Then on that partial view you could have all kinds of operations, such as database access, in one single spot.
Here is an article on how to do exactly this:
http://techbrij.com/981/role-based-menu-asp-net-mvc

Was just looking into something similar. Hopefully partial views are your answer, this is a template you can re use and stick on any page. Information found here.

Related

sitecore mvc ControllerRenderer and the context item Layout

Just playing around with Sitecore 7 and MVC, and I try to get the rendering basics working.
So far, I have been able to create a View Rendering (and mapped to the relevant .cshtml file) within the Renderings section, and applied these to the presentation details of the item (in much the same way you do with ASPX Layouts/ASCX Sublayouts).
I have also been able to map the Item to a controller (using the Controller and Action fields on the item), have the Index action on the controller (inherited from SitecoreController) return the view ~/Views/Home/Index.
The issue I can't seem to wrap my head around is merging the two rendering methods. I want to be able to create controllers that map to an Item, but render the item using the ViewRenderer, rather than using the default MVC conventing of return View(), so that I can:
Specify the location of the view files within a multi-site environment by setting the path parameter of the rendering; and
Have content authors/managers manage the renderings the way that the Layout/Sublayout does with place holders.
Does anyone know of a way that this can be achieved?
Have you taken a look at Controller Renderings in Sitecore MVC? These give you the ability to map a controller class to a Sitecore presentation item that can be statically or dynamically bound to your layout details.
This post has a reasonable overview of how to get started with controller renderings.
As for specifying the location of View files for multi-site environments you can pass the path to the razor file into the Controller View method, for example:
return View("~/Areas/SampleArea/Views/SampleArea/Index.cshtml");
I hope this helps.

How Can I Hide Specific Elements on a Razor View Based on Security without Logic in View?

I have looked all over for elegant solutions to this not so age-old question. How can I lock down form elements within an ASP.Net MVC View, without adding if...then logic all over the place?
Ideally the BaseController, either from OnAuthorization, or OnResultExecultion, would check the rendering form elements and hide/not render them based on role and scope.
Another approach I have considered is writing some sort of custom attributes, so as to stay consistent with how how we lock down ActionResults with [Authorize]. Is this even possible without passing a list of hidden objects to the view and putting if's all over?
Other background info: We will have a database that will tell us at execution time (based on user role/scope) what elements will be hidden. We are using MVC3 with Razor Viewengine. We're utilizing a BaseController where any of the Controller methods can be overridden.
Any help on this would be deeply appreciated!
You could use a number of different methods:
Send the user to a different view (display only view) based on the action filter, or a condition in the controller.
On a field basis, you could build the logic into the editor templates to read custom data-annotations based on role/permission.
You can build HTML helpers to handle the logic and render the appropriate partial view, css class, or text.
For more reading:
How much logic is allowed in ASP.NET
MVC views?
ASP.NET MVC 2 Templates, Part 1: Introduction (there are 5 parts, very informative and a good place to start developing your own editor templates)

Razor Layout Page pre-set values

I'm trying to convert an old masterpage (ASP.NET Webform) into Razor layout for new project. and I just wonder how can I set some values in the Razor layout by calling few other custom function. I know I can just write them in my layout page but it seems a bit messy. What is the best approach?
The best is to have the controller action pass whatever values a view might need in the view model. Another possibility is to use a custom HTML helper which would format the values. Or include a partial: Html.Partial, or render an action: Html.Action. Yet another possibility is to include a #section. So as you can see there are many ways, which one is best would depend on your exact scenario. I can though say which is the worst: write C# code in your views. The view shouldn't set anything. It should be as dumb as possible and simply render whatever information it is thrown to it by a controller.

Best way to create a dynamic Google Sitemap using ASP.NET MVC?

Is there a recommended approach for creating Google Sitemaps using ASP.NET MVC?
I'm new to MVC and this is the first time I've needed to create one and wondered how best to go about it.
I have a number of static links (About Us, FAQ's etc.) that I would like included within the sitemap, but then need the rest of it to be dynamically generated from articles that have been posted on the site.
Any advice/direction on how to create this would be much appreciated.
1 - The first thing you'll need is to create a representation of your entire website a list of nodes which have children, parents and so forth. The easiest way to do this without rolling your own solution is to use the MVCSiteMapProject. It allows you so use MVCish terms like your action and controller names to define nodes which will automatically have the correct urls using your routing definitions.
2 - Now because the MVCSiteMap inherits from the default XmlSiteMap (may not have the exact name right ) you can use another add in to generate a google sitemap from the nodes you've defined in the MVCSiteMapProject.
There a bunch of ways to do #2 so its up to you to decide the technique.
You could create a generic handler the same as web forms, but I'd be inclined to use a controller action and a custom route.
Some simple steps to follow might be:
Create an action in your Home Controller (or create a new one), call it SiteMap.
Have the action return a View with your page data as the model.
Create a View called SiteMap that contains the Google XML, then iterate through your page data to generate the dynamic content.
Add a custom route to your Global.asax file that points to "/sitemap.xml" or whatever and pre-populate your the controller and action parameters with that of your new action.
If you're unsure of custom routes, just copy the default one and paste it above. The routes are handled first come first serve. Make sure you give it a new name.
Rich
for dynamic sitemap, i found this is the best solution : http://ben.onfabrik.com/posts/generating-dynamic-xml-sitemaps-in-aspnet-mvc
it use a controller to generate the xml file.

Providing data to Menu in my ASP.NET MVC Master Page

We are beginning the process of moving from Web Forms to MVC for all of our new applications. I am working on porting our Master Page over and am trying to satisfy the requirements that we need a single master page to be used by all applications. The primary navigation for the application needs to be in a menu within the master page. Accomplishing this was easy, the hard part is that each application may need to determine what to display in the menu using a unique set of rules. Some apps can simply say, here's the menu structure to use via something like a SiteMap. Others need to determine what is displayed in the menu based on what roles the user has, this can also be handled easily with a SiteMap. The situation that I'm struggling with is that some apps need to generate the menus based on the roles the user has, but also on the data on which they are working. i.e. The same user may have different option in the menu for a page if they are working on object 'foo' than they do if working on object 'bar'.
What I've done at this point, is I've created an HtmlHelper that is called by the master page view and takes a list of objects of a custom type and returns an unordered list that is styled by a jQuery plugin to display the menu. The list of objects the helper method takes are passed to the view using the ViewData dictionary. Currently, the value of this ViewData node is set within the constructor of each controller. This allows each page, and potentially each method, to set a different menu without having to set the value in each action method, unless its needed. I have also created a class that parses a SiteMap and returns the list of items needed to build the menu. This class is what I'm using to set the ViewData value in the controller. The idea being that if an application needed more control of how the menu data was generated, they could create their own class to generate the data as long as it returns a list of the correct type of objects.
This solution seems to work fine so far, it just doesn't 'feel' right for some reason. I'm hoping that I can either get some ideas of better way to do this or some reassurance that this is a valid approach to solving this problem.
If it is something that will be on every page, do something like this:
Create a base controller:
public class MyBaseController : Controller
Have this controller get the data it needs and send that data in the ViewData["menu"] to the View. Then have all your controllers inherit from this one:
public class HomeController : MyBaseController
In the Master Page, loop through your ViewData and create your menu.
(I did something like this for my sub-menu which displayed a list of categories.)
In the book I am reading (Pro ASP.NET MVC Framework by Apress) they use Html.RenderAction for the menu in the masterpage. I am a Asp.net MVC novice so maybe somebody else can give more info about this.
You can download the sourcecode at apress.com though so maybe that could help.

Resources