Razor Layout Page pre-set values - asp.net-mvc

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.

Related

controller logic for gsp templates

I'm working on organizing my gsp's into templates. The problem is that some of them require a fair amount of controller type logic (fetching data based on the current request, massaging it before display, etc). Currently it seems like I have two options:
Do the controller type work in the template itself using taglibs and scriptlet. This seems to be a poor separation of View and Controller. Plus it gets quite ugly quite quickly.
Do the controller type work in every controller that ultimately uses that template. This is not very DRY.
Neither of these seem quite MVC to me. What I'm looking for is a way to call a gsp template that has it's own controller. Is there such a thing in grails?
thanks,
The proper way to reuse your template (as you have defined here) is to actually include the call to the owning controller from your other GSP. Such as:
<g:include controller="myFancyControllerName" action="actionThatRendersTemplate" />
In doing so you are ensuring that the logic that builds the model used by the template is kept within the controller you are calling and you don't have to repeat it anywhere else.
You can put a fair amount of logic into taglibs, or call services from a gsp. You don't have to put all your logic in a controller or call a controller from a GSP.

Using #Html.Partial In Sitecore

I would like to use the generic razor helper function Html.Partial to render views that have common html in them.
For instance, I have two views set up in Sitecore Payment Information.cshtml and Agent Payment Information.cshtml. These are rendered using the Sitecore rendering engine. Both of these views have very similar html in them that I would like to put in razor views not set in Sitecore and call them with #Html.Partial as appose to #Html.Sitecore().Rendering() as the latter forces me to set up a view and model in Sitecore which I am not sure is necessary.
My question is, is there anything that Sitecore does behind the scenes that makes it necessary to usethe #Html.Sitecore().Rendering() helper method instead of the #Html.Partial() helper method? Everything seems to work fine and I believe the entire view should get cached since the #Html.Partial call is nested inside either the Payment Information view or the Agent Payment information view set up in Sitecore.
Thanks in advance.
I have Html.Partial working in an MVC solution using Glass for ORM. There are two ways I've used this, one where the assumed model being passed to the partial is the same as the parent rendering and another where we create the model on the fly.
Assumes parent rendering model is passed:
#Html.Partial("~/Views/Components/MyPartialView.cshtml")
Instantiates a new model that is passed in:
#Html.Partial("~/Views/Components/Navigation/SecondaryNavigationRendering.cshtml", new SecondaryNavigation())
The parent view will need to have a mapped model in Sitecore. The secondary view does not have a mapped model in Sitecore but is typed to receive the model being passed (so in my first example that would be my IBasePage model, in my second it would be my SecondaryNavigation model).
Hope this helps.

Creating a navigation for entire website

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.

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)

Render different menus using ASP.NET MVC ActionFilters

I have a standard menu using ul and li tags. And in my database, I have a table Users with a field 'certificate' and depending the value of this 'certificate', the user will see or not some items of the menu.
I was reading some texts and I think I will have to use ActionFilters. Is this right?
So, how can I render different menus depending which user is accessing?
thanks!!
Check out the Html.RenderAction methods that the futures assembly introduces. They can let you, in a very clean fashion, render an action method. This means that you can have a MenuController (for example) that takes care of all of the logic about what menu items you can render. Then it can just pass a simple data structure to the view, whose responsibility it is to render that data structure. Very clean.
I would consider using RenderPartial or RenderAction instead. ActionFilters are not quite suited to that kind of thing. Take a look at this article.

Resources