Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9052617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:13:09+00:00 2026-06-16T13:13:09+00:00

I’ve been looking through Google search results for ways to implement some sort of

  • 0

I’ve been looking through Google search results for ways to implement some sort of RadioButton list (VB, MVC3, Razor). Unfortunately, most of the results I find are in C#; this is the most useful reference I can find: Jon Lanceley’s MVC3 RadioButton Helper (C#, by the way), so I needed to translate the code into VB (thanks, developerFusion!).

I thought it would be fairly straighforward, but I discovered that the compiler doesn’t recognize the function as part of the module namespace; some attempts at working around the problem ensued, but failed.

Here’s my code (you can see all the attempts I’ve tried via comments, signed with Andrew.San if you Ctrl + F it):

Helper:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports System.Web.Mvc.Html
Imports System.Linq.Expressions
Imports System.Runtime.CompilerServices
Imports System.Text

Namespace RadioButtonListForHelperTest
    Public Module HtmlExtensions

        Sub New()

        End Sub

        <Extension()> _
        Public Function RadioButtonForSelectList(Of TModel, TProperty)( _
                                                                          htmlHelper As HtmlHelper(Of TModel), _
                                                                          expression As Expression(Of Func(Of TModel, TProperty)), _
                                                                          listOfValues As IEnumerable(Of SelectListItem)) As MvcHtmlString
            Dim metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData)
            Dim stringBuilder = New StringBuilder()

            If listOfValues IsNot Nothing Then
                ' Create a radio button for each item in the list 
                For Each item As SelectListItem In listOfValues
                    ' Generate an id to be given to the radio button field 
                    Dim id = String.Format("{0}_{1}", metaData.PropertyName, item.Value)

                    ' Create and populate a radio button using the existing html helpers 
                    Dim label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text))
                    Dim radio = htmlHelper.RadioButtonFor(expression, item.Value, New With { _
                     Key .id = id _
                    }).ToHtmlString()

                    ' Create the html string that will be returned to the client 
                    ' e.g. <input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line1</label> 
                    stringBuilder.AppendFormat("<div class=&quot;RadioButton&quot;>{0}{1}</div>", radio, label)
                Next
            End If

            Return MvcHtmlString.Create(stringBuilder.ToString())
        End Function
    End Module
End Namespace

Model:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.ComponentModel.DataAnnotations
Imports System.Web.Mvc

Namespace RadioButtonListForHelperTest.Models
    Public Class TestModel
        Public Property TestRadioList() As IEnumerable(Of SelectListItem)
            Get
                Return m_TestRadioList
            End Get
            Set(value As IEnumerable(Of SelectListItem))
                m_TestRadioList = value
            End Set
        End Property
        Private m_TestRadioList As IEnumerable(Of SelectListItem)
        Public Property TestRadioList2() As IEnumerable(Of SelectListItem)
            Get
                Return m_TestRadioList2
            End Get
            Set(value As IEnumerable(Of SelectListItem))
                m_TestRadioList2 = value
            End Set
        End Property
        Private m_TestRadioList2 As IEnumerable(Of SelectListItem)

        <Display( _
            Name:="Test Radio 1", _
            Description:="This is the first list.", _
            Prompt:="Some prompt message.", _
            Order:=2)> _
        <Required(ErrorMessage:="You must select an option for TestRadio")> _
        Public Property TestRadio() As String
            Get
                Return m_TestRadio
            End Get
            Set(value As String)
                m_TestRadio = value
            End Set
        End Property
        Private m_TestRadio As String

        <Display( _
            Name:="Test Radio 2", _
            Description:="This is the second list.", _
            Prompt:="Some prompt message.", _
            Order:=1)> _
        <Required(ErrorMessage:="You must select an option for TestRadio2")> _
        Public Property TestRadio2() As String
            Get
                Return m_TestRadio2
            End Get
            Set(value As String)
                m_TestRadio2 = value
            End Set
        End Property
        Private m_TestRadio2 As String

        ' I tried it out, just in case 
        ' (see Index; @Html.RadioButtonForSelectList).
        ' It didn't help, really.
        ' - Andrew.San
        Public Function RadioButtonForSelectList(Of TModel, TProperty)( _
                                                                          htmlHelper As HtmlHelper(Of TModel), _
                                                                          expression As Expressions.Expression(Of Func(Of TModel, TProperty)), _
                                                                          listOfValues As IEnumerable(Of SelectListItem)) As MvcHtmlString
            Return RadioButtonListForHelperTest.RadioButtonForSelectList(htmlHelper, expression, listOfValues)
        End Function
    End Class

    Public Class aTest
        Public Property ID() As Integer
            Get
                Return m_ID
            End Get
            Set(value As Integer)
                m_ID = value
            End Set
        End Property
        Private m_ID As Integer
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(value As String)
                m_Name = value
            End Set
        End Property
        Private m_Name As String
    End Class
End Namespace

Controller:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports RadioButtonListForHelperTest.RadioButtonListForHelperTest.Models

Namespace Controllers
    Public Class TestController
        Inherits System.Web.Mvc.Controller

        '
        ' GET: /Test

        Function Index() As ActionResult
            Dim list As New List(Of aTest)()
            list.Add(New aTest() With { _
             .ID = 1, _
             .Name = "Line1" _
            })
            list.Add(New aTest() With { _
             .ID = 2, _
             .Name = "Line2" _
            })
            list.Add(New aTest() With { _
             .ID = 3, _
             .Name = "Line3" _
            })
            Dim sl As New SelectList(list, "ID", "Name")

            Dim list2 As New List(Of aTest)()
            list2.Add(New aTest() With { _
             .ID = 1, _
             .Name = "test1" _
            })
            list2.Add(New aTest() With { _
             .ID = 2, _
             .Name = "test2" _
            })
            Dim sl2 As New SelectList(list2, "ID", "Name")

            Dim model = New TestModel()
            model.TestRadioList = sl
            model.TestRadioList2 = sl2

            model.TestRadio = "2"
            ' Set a default value for the first radio button helper
            Return View(model)
        End Function

        '
        ' POST: /Test

        <HttpPost()> _
        Public Function Index(model As TestModel, returnUrl As String) As ActionResult
            If ModelState.IsValid Then
                ModelState.AddModelError("", "Always force an error to be raised so we can test the postback sets the radio buttons to their last values.")
            End If

            ' If we got this far, something failed, redisplay form 
            Dim list As New List(Of aTest)()
            list.Add(New aTest() With { _
             .ID = 1, _
             .Name = "Line1" _
            })
            list.Add(New aTest() With { _
             .ID = 2, _
             .Name = "Line2" _
            })
            list.Add(New aTest() With { _
             .ID = 3, _
             .Name = "Line3" _
            })
            Dim sl As New SelectList(list, "ID", "Name")

            Dim list2 As New List(Of aTest)()
            list2.Add(New aTest() With { _
             .ID = 1, _
             .Name = "test1" _
            })
            list2.Add(New aTest() With { _
             .ID = 2, _
             .Name = "test2" _
            })
            Dim sl2 As New SelectList(list2, "ID", "Name")

            model.TestRadioList = sl
            model.TestRadioList2 = sl2

            Return View(model)
        End Function

    End Class
End Namespace

View:

@Imports RadioButtonListForHelperTest <!-- Andrew.San - Looked up Google on how to import a namespace in VB -->
@ModelType RadioButtonListForHelperTest.RadioButtonListForHelperTest.Models.TestModel

@Code
    Dim obj1 As System.Web.Mvc.HtmlHelper(Of RadioButtonListForHelperTest.RadioButtonListForHelperTest.Models.TestModel)
    Dim obj2 As System.Web.Mvc.HtmlHelper(Of RadioButtonListForHelperTest.RadioButtonListForHelperTest.Models.TestModel)
    ViewData("Title") = "Index"
End Code

<h2>Index</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@Using Html.BeginForm()
    @Html.ValidationSummary(True)
    @<fieldset>
        <legend>TestModel</legend>

        <div class="editor-label">
            @Html.LabelFor(Function(model) model.TestRadio)
        </div>
        <div class="editor-field">
            @Html.RadioButtonForSelectList(Function(model) model.TestRadio, Model.TestRadioList)
            <!-- Andrew.San
                Apparently, the method isn't a member of 
                System.Web.Mvc.HtmlHelper(Of RadioButtonListForHelperTest.RadioButtonListForHelperTest.Models.TestModel),
                which is odd; I don't see the need for a Model to have this method. 
            -->

            @RadioButtonListForHelperTest.RadioButtonListForHelperTest.RadioButtonForSelectList(obj1, Function(model) model.TestRadio, Model.TestRadioList)
            <!-- Andrew.San
                The compiler needs me to send an htmlHelper argument to the 
                HtmlExtension RadioButtonForSelectList method, 
                which Jon Lanceley didn't need to do.

                @Html.RadioButtonForSelectList(m => m.TestRadio, Model.TestRadioList)
            -->

            @Html.ValidationMessageFor(Function(model) model.TestRadio)
        </div>

        <div class="editor-label">
            @Html.LabelFor(Function(model) model.TestRadio2)
        </div>
        <div class="editor-field">
            <!-- Andrew.San
                As per the first radio button list. 
            -->
            @Html.RadioButtonForSelectList(Function(model) model.TestRadio2, Model.TestRadioList2)
            @RadioButtonListForHelperTest.RadioButtonListForHelperTest.RadioButtonForSelectList(obj2, Function(model) model.TestRadio2, Model.TestRadioList2)
            @Html.ValidationMessageFor(Function(model) model.TestRadio2)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
End Using

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Right. That’s all I’ve got.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:13:10+00:00Added an answer on June 16, 2026 at 1:13 pm

    The problem is that your extension method is in the RadioButtonListForHelperTest namespace, but in your view you attempt to access it view the System.Web.Mvc.Html namespace. @Html is in System.Web.Mvc.

    To access your extension method in the same namespace you need to do the following.

    Change your current namespace to System.Web.Mvc.Html. Then create a static class named whatever you like, MyExtensions and then include you extension method in this class. It must be static.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.