Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to return the SelectList from SelectListItem?
    primarykey
    data
    text
    <p>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 <code>"Cannot implicitly convert type 'System.Web.Mvc.SelectList' to 'System.Collections.Generic.List&lt;System.Web.Mvc.SelectList&gt;"</code>. So kindly help me to convert to SelectList </p> <p>code: </p> <pre><code> public List&lt;SelectList&gt; SelectDoctoronEdit(int id) { Gema_Doctor[] doctorList; doctorList = gema_Doctor.GetDoctor().ToArray(); List&lt;Gema_Doctor&gt; listDoctor = new List&lt;Gema_Doctor&gt;(doctorList); List&lt;SelectListItem&gt; dropItems = new List&lt;SelectListItem&gt;(); RDM_Patient patientsSelect = rdm_Patient.GetPatient().First(c =&gt; c.PatientID == id); dropItems.Add(new SelectListItem { Text = "--Select--", Value = "" }); foreach (Gema_Doctor doctorValues in listDoctor) { RDM_Patient patients = rdm_Patient.GetPatient().First(c =&gt; 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: &lt;tr&gt; &lt;td&gt; @Html.LabelFor(M =&gt; Model.Patient.City) @Html.TextBoxFor(M =&gt; Model.Patient.City) &lt;/td&gt; &lt;td&gt; @Html.LabelFor(M =&gt; Model.Patient.Add1) @Html.TextBoxFor(M =&gt; Model.Patient.Add1) &lt;/td&gt; &lt;td&gt; @Html.LabelFor(M =&gt; Model.Patient.Add2) @Html.TextBoxFor(M =&gt; Model.Patient.Add2) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;/td&gt; &lt;td&gt; @Html.Label("Doctor") @Html.DropDownListFor(m =&gt; Model.Patient.DoctorID.ToString(), (SelectList)ViewBag.doctorType) &lt;/td&gt; &lt;td&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload