I have my view 'Create.cshtml' in folder Views-Department. I want to use partial view which is located in folder on root, 'CommonViews' with the name '_EnterpriseStructure.cshtml'.
I am using syntax RenderPartial
#{Html.RenderPartial("~/CommonViews/_EnterpriseStructure.cshtml");}
Partial view code
<div id="Client">
This is a Partial View.
</div>
When executed I am getting System.InvalidOperationException.
Detail error is:
The view at '~/CommonViews/_EnterpriseStructure.cshtml' must derive from WebViewPage, or WebViewPage.
I am using ASP.Net MVC 5 Razor Views
I have found that Razor view engine is searching in folders 'Views/Shared', 'Views/Departments' . When I moved the Partial View, to folder 'Shared' it worked.
You cannot include path, you just have to give View name. And add folders to search for View engine.
What I cannot figure out is "How to give more Search locations to View Engine?"
I would reckon you to use T4MVC nuget package in your MVC project. Once the package is included in your project, you just need to select "T4MVC.tt" file in your project (solution explorer) and say "Run Custom Tool". That's it. This will create constants for almost everything in your MVC project. That means, all views, controllers, actions, javascript files, resources, css files, images, etc are now having constants.. This way you can avoid using the hard-coded strings for views etc in your methods, and can use constants generated by T4MVC. You won't need to worry about path of views etc. T4MVC is very effective and is must for MVC projects.
Related
I have a template which I render with:
<g:render template="recordInfo" />
in my main-layout. (layout/main.gsp).
That's a template, which some of my views need and some views don't.
For the views, which don't need that template, I put an empty file with the name in the top-level-views directory (/grails-app/views). As described in the grails documentation:
Without the leading '/' it will be first be resolved relative to the
current controller's view directory then, failing that, the top level
views directory.
It works fine for the views which have the template-file in the own view-directory (e.g. /grails-app/views/address). But it doesn't work, where grails should search the template at the /grails-app/views level.
Do I understand something wrong from the documentation or do I have an error in my code?
My file/directory structure looks like:
...
MyApp/grails-app/views/_recordInfo.gsp
MyApp/grails-app/views/address/_form.gsp
MyApp/grails-app/views/address/ ...
MyApp/grails-app/views/address/_recordInfo.gsp
MyApp/grails-app/views/contact/_form.gsp
MyApp/grails-app/views/contact/ ...
...
The address-view needs a customized _recordInfo template and the contact-view don't need it (and therefore I thought grails would pick up the one in the MyApp/grails-app/views directory...)
These three different features exist in the Razor view engine and can be used to achieve similar results. In the end all three of them just render pieces of HTML code, but the way to define and use them is fairly different. I know that:
Html Helpers are created as extension methods for the HtmlHelper class. They frequently use the TagBuilder class to generate some HTML and always should return an IHtmlString.
Razor Helpers (#helper methods) can be defined locally (in the same razor file) or globally (in the App_Code directory). They are small snippets of HTML code that can be reused exclusively in Razor files.
And finally, Partial Views are just regular view files that can be included in other view files using the #Html.Partial helper.
My question is:
Is there a specific scenario for each one of these features? Or it comes down to different flavors to achieve the same result?
HTML Helpers are for reusable components. e.g. WebGrid, Pager, etc. These are distributed as assemblies and have no dependency on Razor. Choose this if:
Functionality is truly reusable and applicable to any application
You don't want people to modify it, or want to version it
Partials Views are a way to split large views into smaller parts to keep things more manageable. They are also useful for reusability that is specific to your application. These are located by the view engine, so you can have the same partial defined in different places (e.g. Views/Shared), allowing you to customize per controller, area or display mode. Choose this if:
Functionality is application-specific
Want to customize per controller, area or display mode
Local Helpers are a way to execute the same template many times, without having to repeat yourself. You can also use it to break views into parts to avoid deep nesting, but keeping everything in the same file. Choose this if:
Functionality is view-specific
Application Helpers (in App_Code) are a mix between local helpers and HTML helpers. Choose this if:
Prefer Razor over TagBuilder
Don't mind distributing files instead of assemblies
Prefer type-safe method-call syntax instead of #Html.Partial(name)
I have created a custom razor helper in my MVC4 Web application that I need to be usable in all of my views.
In all of my view pages, I can't seem to use my custom helper. VS2012 doesn't just see it.
How Can I resolve this please ?
EDIT: It actually works when I run the page, it's just VS that doesn't see it.
Here is my helper which is located in Helpers.cshtml within my AppCode folder.
#helper TextBox(string title, string id, string placeholder, bool required){
<ul>
<li>
<label for="#id">#title</label>
</li>
<li>
<input type="text" name="this" class="#if (#required) {<text>required</text>}" minlength="2" id="#id" placeholder="#placeholder" />
</li>
</ul>
}
Restart Visual Studio
Clean and rebuild alone was not enough but the steps that worked for me were:
Clean solution
Restart Visual Studio (2012)
Rebuild solution
After those steps, the Visual Studio Intellisense picked it up again.
Try to build/rebuild the project (if your helper is in the App_Code folder).
Then VS will recognize the helper.
If it is razor helper(using #helper syntax), you should define it in view placed within \App_Code
We can accomplish this by saving our #helper methods within
.cshtml/.vbhtml files that are placed within a \App_Code directory
that you create at the root of a project. For example, below I
created a “ScottGu.cshtml” file within the \App_Code folder, and
defined two separate helper methods within the file (you can have any
number of helper methods within each file):
And if it is more traditional html helper, you should reference it, by adding record to namespaces element of <system.web.webPages.razor> defined in ~\Views\Web.Config. If you want to use it only in singe view, you could add #using directive on top of view.
In any view you could call your custom Razor helper like this:
#Helpers.TextBox("some title", "someid", "default value", false)
This assumes that your helper is defined inside ~/App_Code/Helpers.cshtml.
My app has the following structure for views...
form related views
views/form/datecontrol.cshtml
views/form/textcontrol.cshtml
views/form/checkboxcontrol.cshtml
...etc
and
search related views
views/search/searchgrid.cshtml
now,in searchgrid.cshtml, I want to make a partial call to the controls under form.
I tried all of the following but it still throws up an error.
#Html.Partial("~/form/textcontrol",
#Html.Partial("/form/textcontrol",
#Html.Partial("views/form/textcontrol",
How do i go about with this ?
Put the shared views in the "Shared" folder, and then reference them as you would any other partial:
#Html.Partial("textcontrol")
If they must be in some other folder, try (be sure to include the ~/):
#Html.Partial("~/Views/form/textcontrol.cshtml")
how i can render Partial views in asp.net mvc 3
I am getting my hands on MVC 3 and am confused that how do i use UserControls in my project.
I have created a usercontrol (cshtml) file called UserControl.cshtml and i am trying to render it Products.cshtml.
MyUserControl.cshtml resides in Shared folder.
In Products.cshtml:
<div>
#Html.Partial("MyUserControl.cshtml");
</div>
But i am getting this error. I dont know why is it trying to search .ascx file.:
The partial view 'MyUserControl.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Products/MyUserControl.cshtml.aspx
~/Views/Products/MyUserControl.cshtml.ascx
~/Views/Shared/MyUserControl.cshtml.aspx
~/Views/Shared/MyUserControl.cshtml.ascx
Is this the correct way to render usercontrol in mvc 3?
--Update--
This works.
#RenderPage("../Shared/MyUserControl.cshtml")
You do not need to specify the file extension, the view engine will handle that.
#Html.Partial("MyUserControl")
Phil haack has fantastic blog on the way to use Partial page.
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx