Click here to Skip to main content
14,265,748 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Friends,
I am Working with Dropdownlist in mvc4 . I need to return a selectlist to bind the dropdown on edit . but I am getting an error "Cannot implicitly convert type 'System.Web.Mvc.SelectList' to 'System.Collections.Generic.List<system.web.mvc.selectlist>" . So kindly help me to convert to SelectList



controller:
public List<SelectList> SelectDoctoronEdit(int id)
{

    Gema_Doctor[] doctorList;
    doctorList = gema_Doctor.GetDoctor().ToArray();
    List<gema_doctor> listDoctor = new List<gema_doctor>(doctorList);
    List<SelectListItem> dropItems = new List<SelectListItem>();
    RDM_Patient patientsSelect = rdm_Patient.GetPatient().First(c => c.PatientID == id);
    dropItems.Add(new SelectListItem { Text = "--Select--", Value = "" });
    foreach (Gema_Doctor doctorValues in listDoctor)
    {
        RDM_Patient patients = rdm_Patient.GetPatient().First(c => c.PatientID == id);
        if (patients.DoctorID == doctorValues.Dr_Id)
        {
            dropItems.Add(new SelectListItem { Text = doctorValues.Dr_Name, Value = doctorValues.Dr_Id.ToString(), Selected = true });
        }
        else
        {
            dropItems.Add(new SelectListItem { Text = doctorValues.Dr_Name, Value = doctorValues.Dr_Id.ToString(), Selected = false });

        }


    }
    //return dropItems;

    SelectList s1 = new SelectList(dropItems, "Value", "Text", patientsSelect.DoctorID);
    return s1;


}



View Page:

<tr>
               <td>
                 @Html.LabelFor(M => Model.Patient.City)
                 @Html.TextBoxFor(M => Model.Patient.City)
                
               </td>
               <td>
                 @Html.LabelFor(M => Model.Patient.Add1)
                 @Html.TextBoxFor(M => Model.Patient.Add1)
                

               </td>
               <td>
                 @Html.LabelFor(M => Model.Patient.Add2)
                 @Html.TextBoxFor(M => Model.Patient.Add2)
                 
               </td>
       </tr> 
       <tr>
              <td>
               
               </td>
               <td>
                @Html.Label("Doctor")
                             
                @Html.DropDownListFor(m => Model.Patient.DoctorID.ToString(), (SelectList)ViewBag.doctorType)
                
                
               </td>
               <td>
                
               </td>
       </tr>
Posted
Updated 28-Nov-16 23:09pm
v5
Comments
Foothill 29-Oct-13 11:59am
   
Since System.Collections.Generic.List and System.Web.Mvc.SelectList do not derive from the same base classes (with the exception of Object), you cannot do an explicit or implicit conversion on them. The easy way would be to make a one-to-one copy between lists.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

the action return type is List<SelectList> but you are returning s1 which is single object not a list
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100