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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:25:43+00:00 2026-05-30T23:25:43+00:00

In my MVC application, I have a helper class designed to render a group

  • 0

In my MVC application, I have a helper class designed to render a group of related buttons. I’m trying to pass in the buttons’ HTML attributes as an anonymous object:

new { @class="myClass1 myClass2" }

The helper emits HTML as an MvcHtmlString, which I’m currently building like this:

        foreach (var b in _buttons)
        {
            sb.AppendFormat("<button type='submit' name='{0}' {1}>{2}</button>",
                b.Value,
                b.HtmlAttributes,
                b.Text);
        }

My problem is that the above code produces invalid HTML:

<button type='submit' name='Foo' { class = myClass1 myClass2 }>Bar</button>

Unfortunately, since it’s passed to the helper as an object, I don’t have type information to work with. I could ToString the object and parse the result, but that seems pretty silly. How can I programmatically convert an anonymous object into key="value" style HTML attributes? Is there an existing utility for this?

  • 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-30T23:25:44+00:00Added an answer on May 30, 2026 at 11:25 pm

    You should use the TagBuilder class, which builds HTML tags for you.

    You can then write

    builder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(HtmlAttributes));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Html Helper file for my ASP.NET MVC application. The majority of
In an ASP.NET MVC application where I have the following model.. public class EventViewModel
I have an MVC application view that is generating quite a large HTML table
I'm working on an asp.net MVC application. I have a class that wraps a
I'm trying to add unit testing to an ASP.NET MVC application I have built.
in my MVC application I have a controller (ProjectController) which has an action (create).
In my ASP .NET MVC application i have a link that refreshes the preview
I have one question; In my ASP.NET MVC web application have to do certain
I have an .Net MVC application which runs fine if I use the build
I have an MVC application that among other things contains a small Silverlight menu

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.