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 8257873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:21:00+00:00 2026-06-08T02:21:00+00:00

I am trying to create a custom LabelFor helper to apply by default instead

  • 0

I am trying to create a custom LabelFor helper to apply by default instead of the standard LabelFor helper include in System.Web.Mvc.Html. I want my LabelFor to take model properties that are PascalCase and make a label that appears as multiple words. For example the property FirstName would appear as “First Name”.

I found this post that shows how to make a custom LabelFor helper that allows the addition of html attributes.

I recreated this helper in VB.Net and modified it to do what I want, but I am not able to get it to work.

Imports System.Linq.Expressions
Imports System.Runtime.CompilerServices

Public Module LabelExtensions

    <Extension()> _
    Public Function LabelFor(Of Tmodel, TValue)(html As HtmlHelper(Of Tmodel), expression As Expression(Of Func(Of Tmodel, TValue))) As MvcHtmlString
        Dim metadata As ModelMetadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData)
        Dim htmlFieldName As String = ExpressionHelper.GetExpressionText(expression)
        Dim labelText As String = If(metadata.DisplayName, If(metadata.PropertyName, htmlFieldName.Split(".").Last()))

        If String.IsNullOrEmpty(labelText) Then Return MvcHtmlString.Empty

        labelText = Regex.Replace(labelText, ".[A-Z]", Function(m) m.ToString()(0) & " " & Char.ToLower(m.ToString()(1)))

        Dim tag As TagBuilder = New TagBuilder("label")
        tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName))
        tag.SetInnerText(labelText)

        Return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal))
    End Function

The sample includes this Module in Namespace System.Web.Mvc.Html but when I add the namespace declaration to this module everything in the rest of the project goes haywire. For example, each of my models has a Primary Key property that is a Guid datatype and as soon as I add the namespace above to the module, I get several errors stating that System.Guid is not defined among other similar errors.

I’ve tried to import my project namespace into a view in order to use the custom helper, but then I get an error that says Overload resolution failed because no 'LabelFor' is most specific for these arguments.

I’m stuck. Can anybody help?

I am trying to avoid having to specify a DisplayName for every PascalCase property I have in many models.

  • 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-08T02:21:01+00:00Added an answer on June 8, 2026 at 2:21 am

    I was able to do this successfully if I named my extension method something other than LabelFor. When I named it LabelForPascalCase I was able to use that function in my views. I think the extension method was not overloading the existing method because they had the same signature.

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

Sidebar

Related Questions

I am trying to create a Custom control derived from a standard Grid. I
I am trying to create a custom user control in WPF. I want to
I'm trying to create a custom membership system in ASP.NET MVC3 . I know
I am trying to create a custom shaped dialog in android. What I want
im trying to create a custom macro program in C# and I want to
I'm trying to create a custom helper like this: # app/controllers/my_controller.rb class MyController <
I am trying to create custom resolver in my ivysettings.xml file: <ivysettings> <settings defaultResolver=default/>
Im trying to create a custom version of the RequiredAttribute to replace the built
I'm trying to create a custom TabItem that is dynamically added to a TabControl
I am trying to create a custom User library in CodeIgniter. Inside this library

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.