Link to home
Start Free TrialLog in
Avatar of NurveTech
NurveTech

asked on

DropDownList Default Selection not working

Problem in a nut shell: This is done with ASP.NET/C#/MVC2

When the page loads it has the proper data in the drop down list, but it is not showing the proper item as being selected.  It defaults to the first item in the list, even though the page source says it should be selecting the 2nd option down.

HELP!!

I need the page to sort the drop down to the Selected="Selected" Item.


Here is the code that renders this section of the page.

<tr>
       <td>
           Complaint Detail:
       </td>
       <td>
           <select id="ComplaintDetail" name="ComplaintTypeID" style="width: 340px">

               <%
                  foreach (var item in CustomerComplaintSystem_MVC2.InformationTypeList.SelectInformationTypeListByBbbTypeID(Model.ComplaintTypeCategoryID))
                   {
                       if (item.InformationTypeListID.Value.ToString() == Model.ComplaintTypeID.Value.ToString())
                       {
                           %>
                       <option value="<%=item.InformationTypeListID %>" selected="selected"><%=item.Name%></option>
               <% }
                     else
                     { %>
                       <option value="<%= item.InformationTypeListID%>"><%=item.Name%></option>
               <% } %>
               <% } %>
           </select>
       </td>
   </tr>

Here is the html after it is rendered.

<tr>
       <td>
           Complaint Detail:
       </td>
       <td>

           <select id="ComplaintDetail" name="ComplaintTypeID" style="width: 340px">


                       <option value="6e2de3e7-7cc9-4d51-be46-bc439c137a64">Inadequate Email Response</option>

                       <option value="5b6dfcf0-537d-401d-a17d-5016c6dd9e98" selected="selected">Complaint - Paralegal Knowledge</option>

                       <option value="7e16ec5c-4612-466c-a10e-740d1a9f03b2">Complaint - Paralegal Demeanor</option>

                       <option value="40bfc518-e6a2-4f13-8eeb-64988d5f085f">Data Entry Errors</option>

                       <option value="88d05997-ce18-4b1b-acfd-606e5d6e93ba">Services Not Performed Adequately</option>


                       <option value="ccd0386f-fe72-4e9f-bf8f-3ec0798fc2c2">Other Customer Service</option>

           </select>
       </td>
   </tr>


Notice how the second option should be the selected option when you look at the page, now check my screen shot and you will see that it just defaults to the top item in the list and doesnt sort down,  


This call gets made on page load to populate the drop down after ddlComplaintType has been poopulated


function GetComplaintTypes() {
       $.getJSON('/Tickets/GetComplaintTypesEdit/?ComplaintTypeID=' + $("#ddlComplaintType").val(), function(data) { $("#ComplaintDetail").fillSelect(data); });
   }


This method sits in my controller and is what i use to populate the dropdown list.



 public JsonResult GetComplaintTypes(Guid ComplaintTypeID)
       {
           List<CustomerComplaintSystem_MVC2.Models.DropDownListData> data = new List<CustomerComplaintSystem_MVC2.Models.DropDownListData>();
           InformationTypeList[] options = InformationTypeList.SelectInformationTypeListByBbbTypeID(ComplaintTypeID);
           DropDownListData dataOption1 = new DropDownListData();
           dataOption1.TextField = "--Select One--";
           dataOption1.ValueField = "0";
           data.Add(dataOption1);
           foreach (var item in options)
           {
               DropDownListData dataOption = new DropDownListData();
               dataOption.TextField = item.Name;
               dataOption.ValueField = item.InformationTypeListID.ToString();
               data.Add(dataOption);
           }
           return Json(data, JsonRequestBehavior.AllowGet);
       }

 User generated image
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Shouldn't it simple be "selected":

<option value="<%=item.InformationTypeListID %>" selected><%=item.Name%></option>

DaTribe
ASKER CERTIFIED SOLUTION
Avatar of NurveTech
NurveTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I thought I would have a stab at it without double checking. Couldn't remember off the top of my head and just assumed it was the same format as checkboxes.

DaTribe
Avatar of NurveTech
NurveTech

ASKER

I solved my own problem.