i want to controller to search for a view in more than two folder (by default controler search in Shared and second folder with its own name ) can we make controller to search in any third folder including above two also.
Custom View Engine
Write a custom view engine and provide all your paths that you want. Then register it in global.asax codebehind in Application_Start event handler.
Check this question on SO that will give you all the information you need:
Can I specify a custom location to "search for views" in ASP.NET MVC?
Related
I have created a partial view in Purchase module, _Attachment.cshtml. So the structure:
View
Purchase
_Attachment.cshtml
Index.cshtml
Sales
Index.cshtml
How to use _Attachment.cshtml in Sales module? I call #Html.Partial("_Attachment") in Sales Index.cshtml, but I've got "The partial view '_Attachment' was not found or no view engine supports the searched locations"
If you want to use a view in another controller, then you will need to move it into the relevant Views/shared folder
So if using areas and you want it to stay in that area
/Areas/{areaname}/Views/Shared
If you want to use it in multiple areas or you aren't using areas, then place it in the root shared
/Views/Shared
#{Html.RenderPartial("~/Views/Purchase/_Attachment.cshtml");}
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.
My Views folder has gotten crazy big! I would like to reorganize it so that the Views folder contains a list of Modules, and then each Module folder contains its share of the View (Controller) folders that currently appear under Views folder.
But of course this means going into each of my controllers and editing every view-returning method the explicit location of its view.
So instead of Controller Orders.Index method just having this:
return View();
I have to edit it to return this:
return View("~/Views/Orders/Index.cshtml");
You can imagine the suck level that this exercise attains over 50 or so controllers.
Is there some way that I can setup a routing or something per controller that will tell that controller's methods to go find their views in a defined subfolder of the Views folder?
It can be done with the help of CustomViewEngine
Follow this post and i hope you can provide your own locations to locate the view template.
MVC provide way where we can easily provide list of path to be searched
Locate view
Once you add CustomViewEngine, register it in Application_Start() event and then you are done :)
Happy coding
You could fix that by implementing a custom RazorViewEngine, where you can specify the search path for the views per request, per controller and so on.
I have 2 controllers which are SearchController and SearchByStaffController respectively. They are very similar and both have an action with action name "Search". When I call View("Search") in their common super class, the confusion comes. Only the "Search" view with SearchController is rendered.
Does the MVC framework get only the first view that matches the name and ignore the rest?
I tried to pass the view path in View() and it worked. Would there be any side effect for doing so? I searched over the web and seems no one has done this before.
Thanks!
Does the MVC framework get only the first view that matches the name and ignore the rest?
Yes. The routing rules are aparsed (top to bottom) and when a rule is matched all end.
I tried to pass the view path in View() and it worked. Would there be any side effect for doing so? I searched over the web and seems no one has done this before.
You can but I don't like that because MVC is based on conventions. So, I see forcing the path of the view a way to broke a convention. Are you sure you can't simply create two routing rules for the two methods? So you can do something like this:
return RedirectToAction("Search", "Controller1");
and
return RedirectToAction("Search", "Controller2");
user932390,
mvc uses convention over configuration. this means that the 'search' view will have to be located in both the:
views/Search
and
views/SearchByStaff
folders respectively. the only way around this is to locate the search view under the views/shared folder, then the viewengine will find it there in both cases and use it (assuming they have the same model).
How can i create a sub-controller in ASP.NET MVC?
i.e:
In controller's directory i have a controller named Users.Fine.//GET:/Users/Index
Inside controlles folder i have a subfolder named Groups,inside that subfolder i have a directory name Account,and inside Account i have a controller named Group.
So,the URL should be:
GET:/Groups/Account/Index
Correct?
But it doenst work,cant find that URL.
It expects:GET:Groups/Index
Any ideias?
You should not have subfolders in a controller. You should have a Controller for Users and a controller for Groups.
In the Users controller you will have a action called index which will give you the /Users/Index view.
In the Groups controller you will have an action called index and an action called Account You then can access these via /Groups/Index and /Groups/Account`.
If you wanted to have more nesting then you can use Areas. An Area will allow you to have a full set of controllers for a "sub folder". For example you can create a area called Group. Then you will have a default "Home" controller and view which would act as the index and you could add new controllers and views for each "sub-subpage" i.e /Groups/Account where group is the area and account is a controller in that area.