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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:45:12+00:00 2026-06-12T11:45:12+00:00

Is it possible to convert ExpandoObject to anonymously typed object? Currently I have HtmlHelper

  • 0

Is it possible to convert ExpandoObject to anonymously typed object?

Currently I have HtmlHelper extension that can take HTML attributes as a parameter. The problem is that my extension also needs to add some HTML attributes so I’ve use ExpandoObject to merge my attributes and attributes that user passes to the function using htmlAttributes parameter. Now I need to pass merged HTML attributes to original HtmlHelper function, and when I send ExpandoObject, nothing happens. So I guess that I need to convert ExpandoObject to anonymously typed object or something similar – any suggestions are welcome.

  • 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-12T11:45:13+00:00Added an answer on June 12, 2026 at 11:45 am

    I don’t think that you need to deal with expandos to achieve your goal:

    public static class HtmlExtensions
    {
        public static IHtmlString MyHelper(this HtmlHelper htmlHelper, object htmlAttributes)
        {
            var builder = new TagBuilder("div");
    
            // define the custom attributes. Of course this dictionary
            // could be dynamically built at runtime instead of statically
            // initialized as in my example:
            builder.MergeAttribute("data-myattribute1", "value1");
            builder.MergeAttribute("data-myattribute2", "value2");
    
            // now merge them with the user attributes 
            // (pass "true" if you want to overwrite existing attributes):
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes), false);
    
            builder.SetInnerText("hello world");
    
            return new HtmlString(builder.ToString());
        }
    }
    

    and if you wanted to call some of the existing helpers, then a simple foreach loop could do the job:

    public static class HtmlExtensions
    {
        public static IHtmlString MyHelper(this HtmlHelper htmlHelper, object htmlAttributes)
        {
            // define the custom attributes. Of course this dictionary
            // could be dynamically built at runtime instead of statically
            // initialized as in my example:
            var myAttributes = new Dictionary<string, object>
            {
                { "data-myattribute1", "value1" },
                { "data-myattribute2", "value2" }
            };
    
            var attributes = new RouteValueDictionary(htmlAttributes);
            // now merge them with the user attributes
            foreach (var item in attributes)
            {
                // remove this test if you want to overwrite existing keys
                if (!myAttributes.ContainsKey(item.Key))
                {
                    myAttributes[item.Key] = item.Value;
                }
            }
            return htmlHelper.ActionLink("click me", "someaction", null, myAttributes);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to convert HTML + CSS into HTML for a system that
Possible Duplicate: Convert HTML to PDF in .NET How can i use the acrobat
Possible Duplicate: Convert JS object to JSON string I have a JSON object in
Is it possible to convert a FF Jetpack extension to a chrome's one? I've
Possible Duplicate: convert string to 2D array using php I have the string like
Possible Duplicate: Convert tags to html entities I am building a snippets area to
Possible Duplicate: Convert HTML to PDF in .NET I am using VSTS 2008 +
Possible Duplicate: Convert some code from C++ to C I've got some code that
Possible Duplicate: Convert multidimensional array into single array I have this array: Array (
Possible Duplicate: Convert flat array [k1,v1,k2,v2] to object {k1:v1,k2:v2} in JavaScript? I want to

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.