Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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:

ASP
<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

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900