Prove me wrong: VB.NET HtmlHelper extension method does NOT work in MVC 4 since VS 2012

No matter how many times I try, I can not get my HTML Helper extension method to work. Here are the testing steps I created if someone wants to try it for themselves:

  • Using Visual Studio 2012, I am creating a new Visual Basic ASP.NET MVC 4 web application using the Internet Application project template.

  • I create the folder "~/Views/Helpers"

  • I create the file "DisplayForPropertyHelper.vb" and add the following code:

     Namespace TestProject.Extensions Public Module HtmlHelperExtensions <Extension()> Public Function DisplayForProperty(helper As HtmlHelper) As MvcHtmlString Return MvcHtmlString.Create("TEST") End Function End Module End Namespace 
  • I open "~/Views/Web.config" and change the following (I add the extension namespace):

     <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Optimization"/> <add namespace="System.Web.Routing" /> <add namespace="TestProject.Extensions"/> </namespaces> </pages> </system.web.webPages.razor> 
  • Compile project

  • Select ANY razor viewing file and enter @Html.Display - and you will see that the extension method is not displayed.

  • Add @Imports TestProject.Extensions razor @Imports TestProject.Extensions , then save and close the file.

  • Return the file and enter @Html.Display , and you will see that the extension method is not displayed.

  • You can even try closing VS2012 and reopening the project. Irrelevant.

I have been struggling with this for several weeks now. All the answers that I found here and in other places, NO help. Someone must have an answer.

+6
source share
2 answers

Make sure you use the prefix of your namespace with your application name. So, for example, if the application you created was called by MvcApplication1 in your ~/Views/web.config , you should put:

 <add namespace="MvcApplication1.TestProject.Extensions"/> 

not only:

 <add namespace="TestProject.Extensions"/> 

Yes, I think this is one of those things in VB.NET :-) Oh, and don't forget to close and open the Razor view after making changes to the ~/Views/web.config , otherwise your changes will not be accepted (if you run the application , it will work, of course).

+10
source

This is actually not an answer, but I am writing it here, hoping only to add a (slightly) overall picture.

For me, I found that my โ€œModuleโ€ class was not explicitly changed as โ€œPublicโ€ (by default, Friend is used when you do not specify a modifier, and that was what killed me)

Yes, I understand the OP example. The module has already been declared Public already .. but I just did not catch it in my code stack and decided that I would highlight this requirement. As a native of C-sharper who has been playing VB.Netter in the last five years, Iโ€™m tired of translating C # fragments manually .. and I usually rely on code converters . Thus, the excellent HtmlHelper that I found and converted did not quite understand, and I forgot to look for this problem in the code snippet that was created for me.

As noted in previous comments, I would also like to join the choir and point out the following:

  • I did all of the above (as I thought, as I checked the OP checklist again and again)

  • I also find that HtmlHelpers is almost impossible for VB.NET MVC 4 (until today) - requirements seem to be supported in such a fragile way

  • I also despise the need to use VB.NET to develop MVC sites (but I have to live with it .. this is part of the legacy code that I inherited from my company)

  • As much as I despise him (for reasons like this), I will say that everything seems to be supported if you are decisive enough to understand this. (thanks StackOverflow!)

Prior to this discovery, I thought it might not work because my application name has dots in it (MyCorp.Web.WorkOrders) compared to (MvcApplication1) .. but, my tests conclude that the application name was not question. In any case, you name it, it will work.

Again, as @Darin already pointed out, you should definitely include the solution name (space) as part of the add namespace declaration in views / web.config

+4
source

All Articles