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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:43:44+00:00 2026-05-29T11:43:44+00:00

I’m looking to create an unobtrusive cascading dropdown system for a website I’m working

  • 0

I’m looking to create an unobtrusive cascading dropdown system for a website I’m working on. I’m having trouble figuring out how to get the various HtmlHelper methods to include the custom html attributes to the rendered tag, though.

Looking through the source for the built in HtmlHelper methods, they all make a call to GetUnobtrusiveValidationAttributes, which creates all the data-val-* html attributes . That’s great if you need the validator attributes, but I’d like to be able to add other attributes this way without needing to alter templates and create new HtmlHelper extensions.

Is this at all possible? Am I overlooking something?

Edit

I know that all the HtmlHelper methods have an overload that accepts an object with html attributes. I’m trying to avoid this if at all possible.

Edit 2

I essentially want this to happen:

public class ViewModel
{
    [Cascading(Action="/Controller/Action")]
    public int Action { get; set; }
}

And then have the HtmlHelpers render out like:

<select data-action="/Controller/Action"></select>

But preferrably without having to write up an extension method to do it. I have no problem making my own helper method to do it, but I’m wondering if I’m missing some built in feature that already looks at random model metadata and can add html attributes.

  • 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-29T11:43:45+00:00Added an answer on May 29, 2026 at 11:43 am

    Seeing edit 1+2, I think you need to create your own extensions. Since you are dealing with dropdowns, you can have a look at this implementation but use custom attributes via IMetadataAware.

    IMetadataAware: This interface can be implemented by an attribute class so that the attribute can add metadata to the model metadata creation process without writing a custom metadata provider. This interface is consumed by the AssociatedMetadataProvider class, so this behavior is inherited by all classes which derive from it, such as the DataAnnotationsModelMetadataProvider class.


    This part is no longer useful as an answer

    If you want to add custom attributes to the generated HTML, you can use the Object htmlAttributes parameter available on many helpers, for example in @Html.ActionLink().

    Example with custom data-* attributes that might be used to unobtrusively initiate a modal dialog for editing user settings on javascript enabled clients. Bootstrap’s modal uses something similar to this.

    Note that I’m using underscores instead of dashes for the data attribute.

    @Html.ActionLink(
        "Settings",
        "CreateOrUpdate",
        "User",
        new { id = "1234" },
        new {
            title = "Edit your personal settings", 
            data_show_modal = "#my-user-settings-modal"
        })
    

    In your case, I guess you are using @Html.DropDownList(...), which takes htmlAttributes as well. Populate them as you like and let your javascript pick up the right data-* attributes.

    public static MvcHtmlString DropDownList(
        this HtmlHelper htmlHelper,
        string name,
        IEnumerable<SelectListItem> selectList,
        string optionLabel,
        Object htmlAttributes
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a jquery bug and I've been looking for hours now, I can't
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.