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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:08:28+00:00 2026-05-30T04:08:28+00:00

What I wants for HTML Output <select name=model binding name> <option value=44 data-object=jsonencoded object>122

  • 0

What I wants for HTML Output

<select name="model binding name"> 
 <option value="44" data-object="jsonencoded object">122 front street</option>
 <option value="45" data-object="jsonencoded object">123 main street</option>
<select>

The line in my cshtml file will look like

@Html.DropDownFillerFor(model => model.ProfileAddressId, 
((ProfileUser)ViewData["ProfileData"]).Addresses, 
profileAddress => profileAddress.AddressID.ToString(CultureInfo.InvariantCulture),
profileAddress => profileAddress.House + " " + profileAddress.Street)

The model is something like a store checkout and you have to select the address to use from a collection of address in ProfileData.

My trouble is understanding how ViewData works inside the helpers and I’m reading the MVC 3 source.

My original solution was:

public static MvcHtmlString DropDownFillerFor<TModel, TProperty, T2Model>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<T2Model> objectList,
Expression<Func<T2Model, string>> valueExp,
Expression<Func<T2Model, string>> textExp,
IDictionary<string, object> htmlAttributes = null)
        {
 var textFunc = textExp.Compile();
            var valueFunc = valueExp.Compile();
foreach (var o in objectList)
            {
                var sli = new SelectListItem {Text = textFunc(o), Value = valueFunc(o)};
                selectList.Add(sli);
            }
            return htmlHelper.DropDownListFor(expression, selectList, null, null);
}

And this was great but lacked the ability to add custom html attributes on a per option basis.
Unfortunately all the the methods that DropDownListFor uses are private so I’m trying to use their code (assuming they know more about this than me). I’m working my way through the code for SelectInternal as found in the SelectExtensions class and I get to the line:

object defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName, typeof(string[])) : htmlHelper.GetModelStateValue(fullName, typeof(string));

This line seems to get the value from the model so in my case the value of model => model.ProfileAddressId but I can’t find a way to reproduce this in my code.

The function GetModelStateValue basically returns the value in ViewData.ModelState.TryGetValue(key, out modelState). Goin a few more levels up the line I find that it all references an “IViewDataContainer ViewDataContainer”. That gets filled in when htmlhelper is created by “HtmlHelper MakeHtmlHelper(HtmlHelper html, iewDataDictionary viewData)” and I can go up a few more levels but this is where I loose it.

My question is. How do I get my values from the model when writing a extension method?

  • 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-30T04:08:29+00:00Added an answer on May 30, 2026 at 4:08 am

    Edit:
    Code for my solution posted at http://pastebin.com/r912xtat It’s been a long time so I don’t remember the details but ask and I can try to help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a HTML select box like below. <select name=item_1> <option value=0>Choose one...</option>
to export some data i want to be able to generate an html output.
I want to conditionally output HTML to generate a page, so what's the easiest
I want to avoid the duplication of stylesheet link tags in the output html
I have the following before_filter : def find_current_membership respond_to do |wants| wants.html { @current_membership
my dynamic generated html look like below <ul class=missingList> <li>Please select at least one
mysql_select_db($database_test2, $test2); $query_Recordset1 = Select color.name As color, item.name As Item, typ.name As Typ,
i used rows.xml() to generate html output. i want to know how to add
i used to select table with class name list. I use this xpath query
I have a a form that I want to output as custom HTML. My

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.