本文介绍了如何从SelectListItem返回SelectList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我在mvc4中使用Dropdownlist。我需要返回一个选择列表来绑定编辑时的下拉列表。但我收到一个错误无法将类型'System.Web.Mvc.SelectList'隐式转换为'System.Collections.Generic.List< system.web.mvc.selectlist> 。很高兴帮我转换成SelectList







控制器:

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>

推荐答案


这篇关于如何从SelectListItem返回SelectList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:27