I'll get this working in C# without any problem. In VB I can get the program to run with the extensions and it's working but intellisense states that 'Span' is not a member of 'System.Web.Mvc.HtmlHelper(Of Object)' so it does not give me any help coding.
The Helper Module:
Imports System.Web.Mvc
Imports System.Runtime.CompilerServices
Public Module HelperModule
<Extension()> _
Public Function Span(ByVal html As HtmlHelper, ByVal text As String) As String
Dim builder = New TagBuilder("span")
builder.GenerateId("firstName")
builder.SetInnerText(text)
Return builder.ToString(TagRenderMode.Normal)
End Function
End Module
And the aspx:
<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="helpertest.helpermodule" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: ViewData("Message") %></h2>
<%= Html.Span("Test")%>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
</asp:Content>
This is just an MVC2 project with nothing else done to it than adding the HelperModule in a new Helpers folder. Am I referencing the Module wrong? I've tried with just the root namespace but that doesn't work either.
I was able to recreate your problem on a new test project and the issue has to do with how vb.net uses namespaces in modules. You need to modify your module to include a namespace like so:
Imports System.Web.Mvc
Imports System.Runtime.CompilerServices
Namespace HtmlHelpers
Public Module HelperModule
<Extension()> _
Public Function Span(ByVal html As HtmlHelper, ByVal text As String) As String
Dim builder = New TagBuilder("span")
builder.GenerateId("firstName")
builder.SetInnerText(text)
Return builder.ToString(TagRenderMode.Normal)
End Function
End Module
End Namespace
Then in your view reference it by the namespace of the application followed by the module namespace, meaning helpertest.HtmlHelpers. So change the import in your view to
ekenman
Member
36 Points
22 Posts
MVC Adding extension to the HtmlHelper in VB
Jun 28, 2011 09:22 PM|LINK
I'll get this working in C# without any problem. In VB I can get the program to run with the extensions and it's working but intellisense states that 'Span' is not a member of 'System.Web.Mvc.HtmlHelper(Of Object)' so it does not give me any help coding.
The Helper Module:
Imports System.Web.Mvc Imports System.Runtime.CompilerServices Public Module HelperModule <Extension()> _ Public Function Span(ByVal html As HtmlHelper, ByVal text As String) As String Dim builder = New TagBuilder("span") builder.GenerateId("firstName") builder.SetInnerText(text) Return builder.ToString(TagRenderMode.Normal) End Function End ModuleAnd the aspx:
<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> <%@ Import Namespace="helpertest.helpermodule" %> <asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server"> Home Page </asp:Content> <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"> <h2><%: ViewData("Message") %></h2> <%= Html.Span("Test")%> <p> To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>. </p> </asp:Content>This is just an MVC2 project with nothing else done to it than adding the HelperModule in a new Helpers folder. Am I referencing the Module wrong? I've tried with just the root namespace but that doesn't work either.
Any help would be appreciated.
Best Regards
Tomas
CodeHobo
All-Star
18647 Points
2647 Posts
Re: MVC Adding extension to the HtmlHelper in VB
Jun 28, 2011 10:19 PM|LINK
I was able to recreate your problem on a new test project and the issue has to do with how vb.net uses namespaces in modules. You need to modify your module to include a namespace like so:
Imports System.Web.Mvc Imports System.Runtime.CompilerServices Namespace HtmlHelpers Public Module HelperModule <Extension()> _ Public Function Span(ByVal html As HtmlHelper, ByVal text As String) As String Dim builder = New TagBuilder("span") builder.GenerateId("firstName") builder.SetInnerText(text) Return builder.ToString(TagRenderMode.Normal) End Function End Module End NamespaceThen in your view reference it by the namespace of the application followed by the module namespace, meaning helpertest.HtmlHelpers. So change the import in your view to
Blog | Twitter : @Hattan
ekenman
Member
36 Points
22 Posts
Re: MVC Adding extension to the HtmlHelper in VB
Jun 29, 2011 06:30 AM|LINK
Hi CodeHobo,
Still does not do it for me. It compiles and run fine, but the intellisense still gives an error.
In intellisense the span command is available, but not as an extension of html.
Could this has to do with some settings somewhere?
ekenman
Member
36 Points
22 Posts
Re: MVC Adding extension to the HtmlHelper in VB
Jun 29, 2011 06:37 AM|LINK
But on my other computer it works....
I've recently installed SP1 and Azure Tools. I guess I should report this on Microsoft Connect....
Wonder83
Member
4 Points
2 Posts
Re: MVC Adding extension to the HtmlHelper in VB
Jul 05, 2011 03:18 PM|LINK
I had the same issue when I installed SP1 to VS2010. It seems to happen only for ASP.NET MVC application written in VB.
ekenman
Member
36 Points
22 Posts
Re: MVC Adding extension to the HtmlHelper in VB
Jul 05, 2011 04:27 PM|LINK
It is when MVC3 is installed. I've upgraded all my applications to MVC3 and that solved the problem. Another solution is to uninstall MVC3.
Wonder83
Member
4 Points
2 Posts
Re: MVC Adding extension to the HtmlHelper in VB
Jul 06, 2011 07:10 AM|LINK
I uninstalled all ASP.NET MVC 3 related entries and now it works. Thank you