Mr A
Mr A

Reputation: 6768

Drop down list doesnt show the selected value in edit mode in mvc 2

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")%>
               <%} %>

Upvotes: 0

Views: 3101

Answers (1)

Ken D
Ken D

Reputation: 5968

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") %>

Upvotes: 1

Related Questions