ASP.NET MVC html helpers not working

Hope someone can help me. I am trying to write a special html helper for my MVC application. Firstly, I tried with a test one that only writes a tag

for the specified parameter. The fact is that it does not work if I explicitly import the namespace. I read a lot, and when I read, this method should appear without an import namespace:

<%=Html.Prueba("This is a paragraph") %> 

But this method, Pruba, does not appear in VS Intellisense.

My class is as follows:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; namespace EasyGoCostaRica.Helpers { public static class ViewsHelpers { //This method is just for testing. Is not working :( public static string Prueba(this HtmlHelper helper, string param1) { return string.Format("<p>{0}</p>", param1); } } } 

Thanks in advance!

+4
source share
3 answers

The namespace must be declared / imported somewhere. You can do it:

  • inside the page itself
  • home page or
  • inside web.config file

If you need something global, the best way is to configure the namespace in web.config.

Use the <@import...> directive for the first two and <namespace> configuration items for the last.

+11
source

You can add a namespace to web.config, and then you won't have to worry about that later.

Inside your web.config you should see something like this:

 <namespaces> <add namespace="System.Web.Mvc"/> <add namespace="System.Web.Mvc.Ajax"/> <add namespace="System.Web.Mvc.Html"/> <add namespace="System.Web.Routing"/> <add namespace="System.Linq"/> <add namespace="System.Collections.Generic"/> </namespaces> 

Just add a line with your namespace.

If you do not want helpers to be imported globally, each directory can have its own web.config. Unless specifically installed, those "sub" web.configs inherit settings from higher web.configs. If you go this route, warn, some settings can only be set at the application level. This can get confusing quickly.

+5
source

For some reason, in visual studio 2013 you need to restart vs for the changes to the web.config application to be applied.

+5
source