Well since most of the tutorials and demos chooses to put the partial view in shared folder im asking for a better way to do this.
My problem:
I want to use partial views in order to create a dynamic interface with reusable views. As i can with usercontrols. Since there might be a lot of partial views i want to put them in a seperate folder than the shared folder.
example:
How it looks today:
[View]
[View.Home]
index.cshtml
[View.Shared]
_layout.cshtml
A better way
[View]
[View.Home]
index.cshtml
[View.Shared]
_layout.cshtml
[View.Shared.Partial]
partial1.cshtml
etc
Or should i rethink my approach enirely? if so how? if not what should i think about?
Here is an example of something I do,
Application Wide used partial views - I put them in my Shared directory.
Partial Views for a specific Controller - I put them in the Views/[ControllerName] Directory.
Related
I have a .Net MVC app with a couple areas, one of which is called Admin. When I execute /Admin/Home/Index, the Index() method in the Admin area Home controller executes and the correct Index.cshtml is returned. So far, so good.
My issue is related to _Layout.cshtml. I want the layout to be different for each area and for the main site. To that end I have added _ViewStart.cshtml to my Admin area's Views folder, and within that _ViewStart I have the following:
#{
Layout = "/Areas/Admin/Views/Shared/_Layout.cshtml";
}
Again, so far, so good, but using a fully qualified path seems inelegant and I have to think there is a better way.
A similar question was asked here:
Area doesn't use the right view
The one answer that was given says that the fully qualified path is the way to go, though it was not marked as the answer by the OP.
So my question is this. Is it necessary to use a fully qualified path to reference views within an area? Is it not possible to do this in an area:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Based on the controller that is executing, is the view engine not capable of resolving that I want the view from the area's shared folder and not the view from the main site's shared folder?
Thanks,
Chris
Your Views inside of the Areas will automatically look to the Views folder in that area for a _ViewStart.cshtml or a _ViewImports.cshtml file. My advice in your case would be to put a _ViewStart.cshtml file into your Area's View folders (Not in their Shared folders!) and have it reference the _Layout file of your choice such as:
_ViewStart.cshtml
#{
Layout = "_Layout";
}
I have a bunch of partial views in my MVC 5 application.
They are used from a bunch of different pages. They have dependencies on certain script files.
Putting scripts in partial views appears to be a big no-no, so I've been putting the scripts in the parent pages.
I haven't been able to figure out how to keep my page rendering logic from being dispersed throughout several source files, and it is annoying to be forgetting references on different pages all the time when I add an existing partial view to them.
It seems like some sort of #include directive would be useful in razor partial views. Has someone bolted this on to MVC in a user contrib?
I like to use forloop htmlhelpers...
In global.asax...
Forloop.HtmlHelpers.ScriptContext.ScriptPathResolver = System.Web.Optimization.Scripts.Render;
In Layout.cshtml...
#Html.RenderScripts()
In Partial View...
#using (var ctx = Html.BeginScriptContext())
{
ctx.AddScriptFile("~/bundles/whatever");
ctx.AddScriptBlock(
#<script>
</script>
);
}
It'd be nice if this package handled css files also...usually a partial view is using some javascript that also requires some css.
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.
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.
Is it possible to declare a function or helper in a Razor layout view and have a child Razor view use that function? My structure is :
_layout.cshtml -> index.cshtml
Where index.cshtml uses _layout.cshtml as its layout. This is the default structure.
I want to be able to place common functions/helpers in _layout.cshtml and refer to them in index.cshtml and all other views that use _layout.cshtml. It doesn't work out of the box unless I've missed something.
I know. I should be using compiled AppHelpers or HtmlHelpers for this, but I can foresee a great convenience in being able to tweak select functions/helpers directly in a _layout.cshtml file.
Thanks
No, that's not possible. #functions are visible only inside the current view.
You could use #helper which could be placed inside the App_Code folder and be reused from all views.
For example you define ~/App_Code/MyHelpers.cshtml:
#helper Foo()
{
...
}
and then in some view:
#MyHelpers.Foo()
But I would recommend compiled helpers. They are unit test friendly and view engine agnostic and when tomorrow Microsoft reveal their brand new Blade view engine you will have 0 work to do in order to use them, whereas if you code against Razor specific things like #functions and #helper you will have to port them. Probably not a concern for you but worth mentioning.