Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Extension Method in View using ASP.NET MVC

I ran into a strange issue over the weekend while I was working on an asp.net mvc project in vb.net. I created an extension method to convert an integer to the corresponding month it is associated with. I tested the extension method in a console application so I know it is working.

In my asp.net mvc project I have a view and want to call the extension method but I get an error that the extension method is not recognized. I imported the namespace it was contained in and still couldn't shake the error. Any idea what's going on? I don't have my code with me, but I can post that tonight if it will help. Thanks!

Extension Method:

Imports System.Runtime.CompilerServices

Module SiteExtensions
    <Extension()> _
    Public Function ConvertToMonth(ByVal monthNumber As Integer) As String
        Dim month As String = String.Empty
        Select Case monthNumber
            Case 1
                month = "January"
            Case 2
                month = "February"
            Case 3
                month = "March"
            Case 4
                month = "April"
            Case 5
                month = "May"
            Case 6
                month = "June"
            Case 7
                month = "July"
            Case 8
                month = "August"
            Case 9
                month = "September"
            Case 10
                month = "October"
            Case 11
                month = "November"
            Case 12
                month = "December"
        End Select
        Return month
    End Function
End Module

View:

<%  For Each m As Integer In DirectCast(ViewData("Months"), IEnumerable)%>
<a href="#"><%=m.ConvertToMonth()%><br /></a>
<%Next%>

Error is: "ConvertToMonth is not a member of Integer"

Jon

like image 593
Jon Avatar asked May 18 '09 12:05

Jon


People also ask

What is extension method in ASP.NET MVC?

What is extension method? Extension methods in C# are methods applied to some existing class and they look like regular instance methods. This way we can "extend" existing classes we cannot change. Perhaps the best example of extension methods are HtmlHelper extensions used in ASP.NET MVC.

What is extension method in VB net?

Extension methods enable developers to add custom functionality to data types that are already defined without creating a new derived type. Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.

What is Razor View extension?

Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write a mix of HTML and server-side code using C# or Visual Basic. Razor view with visual basic syntax has . vbhtml file extension and C# syntax has . cshtml file extension.

What is the function for view on MVC?

In the Model-View-Controller (MVC) pattern, the view handles the app's data presentation and user interaction. A view is an HTML template with embedded Razor markup. Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client.


2 Answers

Make sure you declare your module as Public.

like image 127
David Findley Avatar answered Oct 21 '22 20:10

David Findley


change the code like this:

Public Module SiteExtensions

End Module
like image 32
mhmd Avatar answered Oct 21 '22 18:10

mhmd