MVC Razor helper does not exist in current context - asp.net-mvc

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.

Related

How to load a partial view from ASP.NET MVC application root into area view?

I have an area in my ASP.NET MVC application that needs to make use of partial views stored in the main application.
The code in question is being migrated from the main application into an area for organization, so I need to update the helper tags for the partial views.
Currently my tags look like this:
#await Html.PartialAsync("../Shared/Partials/_details.cshtml")
Of course, this fails in an area, since this helper only begins searching at the Areas/MyArea/ folder. I've tried adding additional ../ to the beginning of the address, but that doesn't change anything. How can I reconnect my partial views to this area?
You need to reference the app root.
The following example references a partial view from the app root. Paths that start with a tilde-slash (~/) or a slash (/) refer to the app root:
Partial Tag Helper:
<partial name="~/Pages/Folder/_PartialName.cshtml" />
<partial name="/Pages/Folder/_PartialName.cshtml" />
Asynchronous HTML Helper:
#await Html.PartialAsync("~/Pages/Folder/_PartialName.cshtml")
#await Html.PartialAsync("/Pages/Folder/_PartialName.cshtml")
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-6.0
See also: significance of tilde sign

Html.RenderPartial - Render Partial View located in another folder

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.

I need an explanation about how to use Grails (with different controllers)

I'm trying to create a little and basical Twitter-like site with Grails.
Here is the main part of my project arborescence (UserCwitter handles users, MessageCwitter handles messages and GroupCwitter handles groups like followers/followings) :
I'm trying to insert a text field to write a new message in the index (here it's index_final.gsp).
So I added this piece of code (in every controller my function to create a new user/message/groupe is called save()) :
<g:form action="save">
<fieldset class="form">
<g:render template="form"/>
</fieldset>
</g:form>
But I don't know why, the form that appears is the one to create a new user and not a message.
Why and what should I do ?
Thanks for your help. Sorry if this is something really easy or even stupid, I'm really new to Grails.
From render tag documentation, related to template attribute:
Note that if the value of the template attribute starts with a '/' it
will be resolved relative to the views directory. This is useful for
sharing templates between views. 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.
So you should use
<g:render template="/messageCwitter/form"/>
if you want to render form template that is in messageCwitter folder.

MVC template from folder other than default (EditorTemplates/DisplayTemplates)?

Can you point MVC to a folder other than the default ones (Views/Shared/EditorTemplates & Views/Shared/DisplayTemplates)? I'd like to either put them in subfolders below those, or in other folders outside the Shared folder.
For example, if I have an editor template under this folder:
~\Views\Order\ProductModel.cshtml
How can I tell my EditorFor template to use this tempalte name?
I tried fully qualifying it, but this doesn't work:
#Html.EditorFor(m => m.ProductModel, #"~\Views\Order\ProductModel.cshtml")
I've tried using forward slashes & backslashes, with/without .chstml, every combination I can think of. I'm beginning to think this isn't supported, but I can't imagine why it wouldn't be.
No, I am afraid you can't do this.
For example, if I have an editor template under this folder
That's no longer an editor template. It's a partial. If you want to share editor templates between different controllers you can simply put them inside the ~/Views/Shared/EditorTemplates folder.
As far as subfolders are concerned then you could do this:
#Html.EditorFor(x => x.Foo, "Order/ProductModel")
which will render the ~/Views/CurrentController/EditorTemplates/Order/ProductModel.cshtml or ~/Views/Shared/EditorTemplates/Order/ProductModel.cshtml editor template.
Old question, but... proper way to add display/editor template for specific controller is to add it in DisplayTemplates or EditorTemplates subfolder.
Assuming that, in your example, you have OrderController, you can simply put display template for your model into sub-folder, like this:
~\Views\Order\DisplayTemplates\ProductModel.cshtml
Then, call display template normally from your view (ex: from `~\Views\Order\Index.cshtml):
#Html.DisplayFor(m => m.MyProductModelProperty)
If you do this:
#Html.EditorFor(x => x.Foo, "Order/ProductModel")
it won't parse Foo as a collection and apply your editor template to each item. It will rather assume that your editor template should be applied to the collection as a whole.
If you want to apply editor template to each item individually, just place it in EditorTemplates folder under your view folder (as it will have precedence) and use the default syntax:
#Html.EditorFor(x => x.Foo)
Of course, the name of the editor template has to match the type of items in your collection.

render usercontrol (cshtml) using #Html.Partial

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

Resources