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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:51:35+00:00 2026-05-20T22:51:35+00:00

I was unable to figure out how to use @class inside Html.LabelFor, so I

  • 0

I was unable to figure out how to use @class inside Html.LabelFor, so I updated an extension helper for Html.Label with code found on SO.

My LabelExtension.cs file has the following class:

 public static class LabelExtensions
    {
        public static string Label(this HtmlHelper helper,
                                string labelFor,
                                string value,
                                object htmlAttributes)
        {
            TagBuilder labelBuilder = new TagBuilder("label");
            if (!string.IsNullOrEmpty(labelFor))
            {
                labelBuilder.Attributes.Add("for", labelFor);
            }
            labelBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            labelBuilder.SetInnerText(value);
            return labelBuilder.ToString(TagRenderMode.Normal);
        } 
    }

I originally used the following in a MVC2 .aspx page thusly (which I am converting to .cshtml):

<% Html.BeginForm(); %> <%= Html.Label("txt_name_udq", "Your First Name (required):",
                new {
                @class
                = "mylabelstyle" } )%>
                <br />
                <%= Html.TextBox("txt_name_udq", null, new {
                @class
                = "myinputstyle" } )%>
                <br />
                <%= Html.Label("txt_email_udq", "Your E-Mail Address (required):", new {
                @class
                = "mylabelstyle" } )%>
                <br />
                <%= Html.TextBox("txt_email_udq", null, new {
                @class
                = "myinputstyle" })%>
                <br />
                <% Html.EndForm(); %>

This rendered very well in MVC2 (I left out usings and such for brevity) . However, today while converting to .cshtml (using a _layout.cshtml) I found that the Html.Label is not rendering and instead outputting source. Here is the code:

@section QuoteForm
{
    <div class="entryfull">
        <div class="entryfull_box">
            <div class="entryfull_text">
                <h2>
                    Quote Request
                </h2>
                <ul class="ul-check">
                    <li>Free</li>
                    <li>Confidential</li>
                    <li>Anonymous</li>
                    <li>No Obligation</li>
                </ul>
                @using (Html.BeginForm())
                {
                    @Html.Label("txt_name_udq", "Your First Name (required):", new { @class = "mylabelstyle" })
                    <br />
                    @Html.TextBox("txt_name_udq", null, new { @class = "myinputstyle" })
                    <br />
                    @Html.Label("txt_email_udq", "Your E-Mail Address (required):", new { @class = "mylabelstyle" })
                    <br />
                    @Html.TextBox("txt_email_udq", null, new { @class = "myinputstyle" })
                    <br />
                }
            </div>
        </div>
    </div>
}

The above is just something simple, not final product. I am just trying to get it to render first. Note: 1) I tried various iterations of Html.BeginForm; and 2) I even enclosed it in . Still, its not “working”.

Here is what you see on the browser for the label (source outputted in browswer), right above the textbox (which renders):

<label class="mylabelstyle" for="txt_name_udq">Your First Name (required):</label>;

And if you “View Source”, here is what you see:

&lt;label class=&quot;mylabelstyle&quot; for=&quot;txt_name_udq&quot;&gt;Your First Name (required):&lt;/label&gt;;

Is this something to do with @section? Or is it that I am trying to use an extension from MVC2?

Thanks in advance for any thoughts/advice.

EDIT: This is the code I reference in the comment that worked in my situation. Thanks for the help.

public static MvcHtmlString Label(this HtmlHelper htmlHelper, string labelFor, string value,
                                      object htmlAttributes)
    {
        var tagBuilder = new TagBuilder("label");
        tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        tagBuilder.MergeAttribute("for", labelFor.Replace(".", tagBuilder.IdAttributeDotReplacement), true);
        tagBuilder.SetInnerText(value);
        return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
    }
  • 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-20T22:51:36+00:00Added an answer on May 20, 2026 at 10:51 pm

    That is because your helper is returning a string and that is encoded by default. Your helper should return MvcHtmlString and change the return statement to

     return new MvcHtmlString(labelBuilder.ToString(TagRenderMode.Normal));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey i am unable to figure out why this xml is not being generated
The cascading dropdown control works great except that I am unable to figure out
Silly question, but I'm unable to figure out.. I tried the following in Ruby:
As title said, I'm trying to figure out how to use javascript and jquery
Here's a very small issue that I was unable to figure out. I'm sure
This seems like it should be really simple, but I'm unable to figure this
I'm having a little trouble with some casts that i cant figure out out
I am trying to use the basic progress bar however I am unable to
OSGi cannot find my DLL file, and I can't seem to figure out why.
When I paste this code into a REPL, it works fine: (use 'clojure.contrib.seq-utils) (defn-

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.