2
votes

Using VB.NET with MVC3 and Razor.

I have a test page for a custom extension to htmlhelper. Visual Studio tells me the view is not ok :

code of index.vbhtml (view)

<h2>@ViewData("Message")</h2>

@Html.CustomLink("text 321312", 123)

Error : CustomLink is not a member of System.Web.Mvc.HtmlHelper(..)

code of LinkExtension.vb (extension)

Imports System.Runtime.CompilerServices
Imports System.Web.Mvc

Public Module htmlHelperExtensions
    <Extension()> _
    Public Function CustomLink(htmlHelper As HtmlHelper, linkText As String, uuid As Short) As MvcHtmlString
        Return MvcHtmlString.Create(String.Format("<a href="#{1}">{0}</a>", linkText, uuid )
    End Function
End Module

web.config (added namespace)

I added a reference to my librairy in both general web.config and view web.config
(I also tried to add it to the controller.)

<system.web>
  <pages>
    <namespaces>
      [...]
      <add namespace="linkExtension" />
    </namespaces>
  </pages>
</system.web>

The page run correctly and build the html as I need it. But VS keeps telling me the code is not right (CustomLink is not a member of 'System.Web.HtmlHelper(Of Object)')! Any ideas?

1
Sometimes after adding a namespace to the web.config file in the Views folder, it is necessary to unload the project, and reload it to get Intellisense for a new helper class.counsellorben
I added the namespace to the web.config in the view folder, but it still give me the same error.Kraz

1 Answers

1
votes

Try specifying the project when adding the namespace:

<add namespace="MyProject.linkExtension" />

If you're unsure what it is, locate the project where your htmlHelperExtensions is, right-click it in the Solution Explorer, select Properties, and on the Application tab use the value under the "Root namespace" label.

Also, your code doesn't show the usage of Namespace linkExtension defined in the extension file, so if the above doesn't work try adding it to your module:

Namespace linkExtension
    Public Module htmlHelperExtensions
        <Extension()> _
        Public Function CustomLink(htmlHelper As HtmlHelper, linkText As String, uuid As Short) As MvcHtmlString
            Return MvcHtmlString.Create(String.Format("<a href="#{1}">{0}</a>", linkText, uuid )
        End Function
    End Module
End Namespace

If that doesn't help then what version of MVC are you running on? A similar issue was reported by someone also using VB.NET. In that person's case they got it working when upgrading all their projects to MVC 3, or uninstalling MVC 3 since they were using MVC 2. Apparently there was also some interference with having SP1 and Azure Tools installed.