locked
MVC Adding extension to the HtmlHelper in VB RRS feed

  • Question

  • User1775971595 posted

    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.

    Any help would be appreciated.

    Best Regards

    Tomas

    Tuesday, June 28, 2011 5:22 PM

Answers

  • User1775971595 posted

    But on my other computer it works....

    I've recently installed SP1 and Azure Tools. I guess I should report this on Microsoft Connect....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 29, 2011 2:37 AM

All replies

  • User1904378495 posted

    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 
     

    <%@ Import Namespace="helpertest.HtmlHelpers" %>
    Tuesday, June 28, 2011 6:19 PM
  • User1775971595 posted

    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?

    Wednesday, June 29, 2011 2:30 AM
  • User1775971595 posted

    But on my other computer it works....

    I've recently installed SP1 and Azure Tools. I guess I should report this on Microsoft Connect....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 29, 2011 2:37 AM
  • User-582012998 posted

    I've recently installed SP1 and Azure Tools

    I had the same issue when I installed SP1 to VS2010. It seems to happen only for ASP.NET MVC application written in VB.

    Tuesday, July 5, 2011 11:18 AM
  • User1775971595 posted

    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.

    Tuesday, July 5, 2011 12:27 PM
  • User-582012998 posted

    I uninstalled all ASP.NET MVC 3 related entries and now it works. Thank you Smile

    Wednesday, July 6, 2011 3:10 AM