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

  • Home
  • SEARCH
  • 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 7864811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:53:49+00:00 2026-06-02T23:53:49+00:00

I have this code: @Html.DropDownList(ddlCouponsAppointment, Model.Coupons.Select(a => new SelectListItem { Text = a.Name, Value

  • 0

I have this code:

@Html.DropDownList("ddlCouponsAppointment",
    Model.Coupons.Select(a => new SelectListItem {
                                    Text = a.Name,
                                    Value = a.CouponId.ToString(),
                                    Selected = (a.CouponId == Model.CouponForAppointmentReminders.CouponId)
                        }), 
    "None",
    new { @class = "normalcell" })

This has worked for ages. Now, it throws an error, which a google tells me is because I am not allowed to call ToString on an entity that is being mapped to a class ( this class has not changed, so I am at a loss how it worked before ). So I added a property called CouponIdStr, which is a string representation. This does not work ( blows up b/c the property is not mapped to the DB ). I tried making value equal string.Empty + a.CouponId ( as C# would concatenate that as a string ). It blows up.

So I tried this:

@Html.DropDownList("ddlCouponsAppointment",
    new SelectList(Model.Coupons,
        "Name",
        "CouponId",
        Model.CouponForAppointmentReminders.CouponId),
    "None",
    new { @class = "normalcell" })

This works, but does not show the selected item. Does anyone have any suggestions ?

  • 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-02T23:53:51+00:00Added an answer on June 2, 2026 at 11:53 pm

    step 1. Create a ViewModel that has the selected value

    step 2. In that ViewModel have the list of items you want in the select list

    public class ViewModel {      
       public int Selected {get;set;}
       public IEnumerable<Option> Options {get;set;}
    }
    

    step 3. In your view, use the DisplayFor method

    Html.DisplayFor(m => m.Selected, new SelectList(Model.Options, "Id", "Display"));
    

    There you have it, when the dropdown renders, it will have the selected value selected!

    Good Luck!

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

Sidebar

Related Questions

I have this HTML code for radios: <input type='radio' name='a_27' value='Yes' id='a_27_0' /> <input
I have follwoing code @Html.DropDownList(optionsforuser, new SelectList(new[] { Option1, Option2 }), Select) Now i
I have a dropdownlist that looks like this: @Html.DropDownListFor( x => x.Attribute.AttributeID, new SelectList(Model.Attributes,
OK i have this code HTML: <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js> </script> <script type=text/javascript>
I have this html code <html> <head> <title>JQuery Problem 2</title> <script type=text/javascript src=jquery-1.4.min.js></script> <script
I have this HTML code which simulates a dropdown multi-checkbox <div> <div class=select> <span>Select
I have this html code: <div class=action> some_text <a class=delete_action name=q1>some</a> </div> <div class=action>
I have this code to set the default value of my select list: public
i have this code <div id=aggregate style=display:inline> <%=Html.RadioButton(a, 1, true, new {id = meRadio})%><label>Me</label>
First the code might suck as I'm new with JQuery. I have this html:

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.