I am wondering if it is possible to open a view in a new window but without the masterpage defined in the header? would I just define a separate view? or is there a better way?
thanks!
Just create an empty Master page, and pass the name of that Master page when returning a ViewResult:
return View("MyView", "Empty"); // Use the "Empty" Master page to render the view
Related
I am newbie in MVC and trying to create application in asp.net mvc2. For the common header and footer i wants to use master pages which are having dynamic content, Can anyone please tell me where is the best place to write code for master page. I am trying to add code behind for master page but I am not sure about what is recommended in this situation.
In my opinion, you can create partial views (type of user control) and render it in your master page. Write all the logic to get data from database into partial view's action method. You can write action method in any of your controller. ex. You can create a Layout controller and write action method related to master page. A partial view can also be reusable.
To render your partial view in master page you can write code like :
<div><%= Html.Action("Header", "Layout") %></div>
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.
Is it possible to have a master page that has a renderpartial it in and supply a value to be passed to that partial before it is rendered.
For example we have a common menu structure across our site, I would like to put this as a partial in the master page, the problem comes in that depending on what area in the page you are we need to set a value for the selected tab so it gets highlighted and you can see where you are.
Currently the render partial is on ever page and passes in a value for the selected tab, I would like this to be in the master page and just have some way to pass the value to the master page.
I have a feeling this might be possible with the changes introduced with razor but I'm not sure, in any case we are using asp.net mvc 2 (although I'm still interested in things that would apply to more recent versions)
You could use RouteData property to get currently called page using the following:
ViewContext.ParentActionViewContext.RouteData.Values["controller"]
ViewContext.ParentActionViewContext.RouteData.Values["action"]
// OR
ViewContext.RouteData.Values["controller"]
ViewContext.RouteData.Values["action"]
Either in the controller associated with the menu or in the view itself (if it's partial). Then you can filter the output upon the current route.
I have a hybrid ASP.NET WebForms/MVC project. In my Master Page, I have a "menu" User Control and a "footer" User Control. Anyways. I need to pass some data (2 strings) to my "menu" User Control on my Master Page (to select the current tab in my menu navigation, etc.) My views are strongly-typed to my data model. How can I push data from my controller to my menu or at least allow my master page to access some data pre-defined in my controller?
Note: I understand this violates pure ASP.NET MVC, but like I said, it is a hybrid project. The main purpose of my introduction to ASP.NET MVC into my project was to have more control over my UI for certain situations only.
Put your strings into the ViewData collection,
ViewData["MenuString1"] = "My First String";
ViewData["MenuString2"] = "My Second String";
and retrieve them in the Master Page like this:
myMenu.Property1 = ViewData["MenuString1"].ToString();
myMenu.Property2 = ViewData["MenuString2"].ToString();
http://nerddinnerbook.s3.amazonaws.com/Part6.htm
You can use ViewData to share data between the Controller and View that is not in the model. In the Controller, do something like ViewData["menu"] = myMenu; and then in the View do <%= ViewData["menu"] %>. You can pass objects but you need to cast the ViewData[key] back to the object type in the View.
Another way to do this is to put your menus and other non-Model related data needs into a separate controller. Then you can use RenderAction to call the action in your navigation controller that generates the menu. Hack has a blog post on RenderAction that explains this in more detail.
I lean towards using ViewData for temporary values from the controller like select lists and a RenderAction for unrelated things such as the main site navigation.
I want allow the user to customize their web pages on my ASP.NET MVC website, so they can use their own style sheet. In MVC, the stylesheet link is placed in the section of the master page. How can I dynamically link to CSS in master page based on the user (that is, I will look up my table to find the CSS for a specific user and link to it).
Thanks!
I would use a ContentPlaceHolder in the tag (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.contentplaceholder.aspx) for the master page and apply the data you want per the View you're showing.
I would have your view specific view models inherit from a common base view model, then have a property on this base view model that gets set in your controllers. Your masterpage can then set the correct css link dynamically without having to repeat code in a ContentPlaceHolder on every single view.
how about using the old school passing the path to your custom controller in the src tag
with your MyCssController redirecting to the custom path based on your logic ??