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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:03:23+00:00 2026-05-30T12:03:23+00:00

How do I create an ASP.Net MVC Helper for an Html.Label which takes in

  • 0

How do I create an ASP.Net MVC Helper for an Html.Label which takes in attributes?

Currently when I define an Html.TextBox I am able to pass in a list of attributes. Sort of like below:

new {disabled="disabled", @class="pcTextBoxWithoutPaddingDisabled"})%> 

However, it does not look as though the Html.Label has this feature. As a result, I have to define my labels using the label tag. Sort of like below:

<label class="pcLabelBlackWithoutPadding">

I would like to be consistent I how my Html element get created.

So, how do I create an Html.Label that will take in a list of attributes?

Thanks for your help.

  • 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-05-30T12:03:25+00:00Added an answer on May 30, 2026 at 12:03 pm

    I have modified Alexandr code a bit with lambda expression, in case anyone needed the lambda expression.

    usage:

    @Html.LabelFor(model => model.Property , new { @class = "bigFont" })
    

    code:

    public static MvcHtmlString LabelFor<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression, Object htmlAttributes)
        {
            var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    
            var innerText = metadata.DisplayName ?? metadata.PropertyName;
    
            if (String.IsNullOrEmpty(innerText))
            {
                return MvcHtmlString.Empty;
            }
    
            var tagBuilder = new TagBuilder("label");
            tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(metadata.PropertyName)));
            tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            tagBuilder.SetInnerText(innerText);
    
            return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I create a dropdown list by using ASP.NET MVC helper like this, <%=Html.DropDownList(Group,-please select
I am using ASP.Net MVC along with Jquery to create a page which contains
Is there a way to make helper in Asp.Net MVC to wrap other html
Can i create html helpers in Webforms project like in asp.net mvc? Thanks.
I've created a few html helper methods for asp.net mvc that I would like
There are two ways of creating a custom HTML helper in ASP.NET MVC: You
I am trying to create my own helper function in ASP.NET MVC 3. Not
I'm trying out ASP.NET MVC Framework and would like to create an ajax helper
I'm currently creating some custom helper classes, similar to ASP.NET MVC's standard HtmlHelper .
I know there is a ListBoxFor extension method among the ASP.NET MVC Html helper

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.