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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:32:39+00:00 2026-05-23T07:32:39+00:00

Hi I am trying to display the database value on the dropdownlist in the

  • 0

Hi I am trying to display the database value on the dropdownlist in the edit section, but the drop down list shows the default set value below is my code:
Controller:

 public ActionResult Edit(int id)
    {
       // Product helmet = new Product();//
        //Product garrage = new Product();

        ViewBag.mode = "edit";
        // for dropdown track
        ITrackRepository trackResp = new TrackRepository();
        IQueryable<Object> tracks = trackResp.GetVenuesSelectlist();
        ViewData["Venue"] = new SelectList(tracks, "VenueID", "Name");

        // for dropdown for event type
        ITrackdayRepository trackdayResp = new TrackdayRepository();
        IQueryable<EventType> eventTypes = trackdayResp.GetAllEventTypes();
        ViewData["EventTypes"] = new SelectList(eventTypes, "ID", "Name");

        // for dropdown experience
        IExperienceLevelRepository expLevelResp = new ExperienceLevelRepository();
        IQueryable<ExperienceLevel> expLevel = expLevelResp.GetAllExperienceLevels().OrderBy(ExperienceLevel => ExperienceLevel.Name);
        ViewData["Experience"] = new SelectList(expLevel, "ID", "Name");

        // dropdown for helmets
        IProductRepository prodResp = new ProductRepository();
        Product productQuantity = prodResp.GetProd(id);

        if (productQuantity.ProductTypeID == 1)
        {
            // dropdown for attendees
            var attendees = Enumerable.Range(1, 80).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
            ViewData["attendees1"] = new SelectList(attendees, "Value", "Text",**productQuantity.QtyAvailable)**; //productQuantity.QtyAvailable is the value from db(selected value of dropdown)

            ViewData["txtAttendees"] = productQuantity.UnitCost;
        }


        else
        {
            var emptyattendees = Enumerable.Range(1, 80).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
            ViewData["attendees1"] = new SelectList(emptyattendees.ToList(), "Value", "Text");


        }    Event trackday = trackdayResp.GetEvent(id); //returns all the values from event table whose eventid is id

        //need to return product quantity, value to drop downlist
       return View("Create", trackday);

    }

View Edited(WOrking):

  <% if (ViewBag.mode != "edit")
                  { %>

                <%: Html.DropDownList("attendees1", ViewData["attendees1"] as SelectList, "--select--")%>
               <%}else{%>
               <%: Html.DropDownList("attendees1")%>
               <%} %>
  • 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-23T07:32:39+00:00Added an answer on May 23, 2026 at 7:32 am

    I had the same problem a month ago, and I solved it by doing this:

    ViewData["attendees1"] = new SelectList(attendees, "Value", "Text", productQuantity.QtyAvailable);
    

    I mean, you have to add a 4th parameter with the SelectedValue which you take it from the original value before the edit. You have to do this only in Edit action, no need to do that in Create since it is a new object and no value is selected yet.

    And in your markup you define the DropDownList like this:

    <%: Html.DropDownList("attendees1") %>
    

    This way the selected value will be selected instead of the default one.

    Hope that helps.

    EDIT:
    Create action method:

    ViewData["attendees1"] = new SelectList(attendees, "Value", "Text");
    

    Edit action method:

    ViewData["attendees1"] = new SelectList(attendees, "Value", "Text", productQuantity.QtyAvailable);
    

    Markup in both Create and Edit views

    <%: Html.DropDownList("attendees1") %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the $url value to display from the MySQL database but
I'm trying to display a list coming from my database using Linqtosql but I
I'am trying to display data from the database file which has the value Age
I'm trying to display set of data which have retrieved from the sql database
I am trying to display from a MySQL Database using PHP and Ajax but
I'm trying to display data from a database and I want to know what
I have a product database and I am displaying trying to display them as
HI, I am trying to display results from a database in results.php. In the
OK folks, I am trying to display the contents of a database table along
I am trying to get a pie chart display correctly but have found there

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.