Trying to bind Dropdownlist from db getting error

hi i am trying to bind Dropdownlist from value coming from db getting error
details are as below
Model

public class Ipcell
{  public List<ddlupc> upcList { get; set; }
}
    public class ddlupc
    {
     public string UscCode { get; set; }
    public string UscDesc { get; set; }
    
}

controller
is being called from jquery Ajax value is coming
public PartialViewResult RaiseBill(List<Ipcell> casedetail1)
        {List<Ipcell> result1 = new List<Ipcell>();
            conn.Open();
            string qry1 = "select IUM_USC_CODE,IUM_USC_DESC from V_IP_USC_MAST";
            OracleCommand command1 = new OracleCommand(qry1, conn);
            OracleDataReader reader1 = command1.ExecuteReader();
            List<ddlupc> objlist = new List<ddlupc>();
            Ipcell member1 = new Ipcell();
            while (reader1 != null && reader1.Read())
            {               

            objlist.Add(new ddlupc { UscCode = reader1[0].ToString(), UscDesc = reader1[1].ToString() });
             result1.Add(member1);
            }
            member1.upcList = objlist;
            conn.Close();
  if (casedetail1!=null)
            {
                ViewBag.Caseid = casedetail1[0].CaseId;
            }
            
            return PartialView("RaiseBill", result1);          

        }

view
@model Smart_M.Models.Ipcell
<div>
<div style="padding-left:5%">Usc Code</div><span style="padding-left:5%">@Html.DropDownList("upcList",Model.upcList) </span><br />
</div>

Getting Error “Cannot convert from ‘System.collections.Generic.List<Smart_M.Models.ddlupc> to ‘string’.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Getting Error “Cannot convert from
‘System.collections.Generic.List<Smart_M.Models.ddlupc> to ‘string’.
Any idea would be appreciated.

Because the data source bound to the @Html.DropDownList is of type IEnumerable<SelectListItem>, but Model.upcList is of type List<ddlupc>,which caused an error.

To solve it, you can convert Model.upcList into IEnumerable form, as follows:

@Html.DropDownList("upcList", new SelectList(Model.upcList, "UscCode", "UscDesc"))

You can also refer to this for more ways to bind dropdownlist in asp.net core.

Update

public PartialViewResult RaiseBill(List<Ipcell> casedetail1)
        {List<Ipcell> result1 = new List<Ipcell>();
            conn.Open();
            string qry1 = "select IUM_USC_CODE,IUM_USC_DESC from V_IP_USC_MAST";
            OracleCommand command1 = new OracleCommand(qry1, conn);
            OracleDataReader reader1 = command1.ExecuteReader();
            List<ddlupc> objlist = new List<ddlupc>();
            Ipcell member1 = new Ipcell();
            while (reader1 != null && reader1.Read())
            {               

            objlist.Add(new ddlupc { UscCode = reader1[0].ToString(), UscDesc = reader1[1].ToString() });
             result1.Add(member1);
            }
            member1.upcList = objlist;
            conn.Close();
  if (casedetail1!=null)
            {
                ViewBag.Caseid = casedetail1[0].CaseId;
            }

            ViewData["dropSrc"] = objlist ;
            return PartialView("RaiseBill", result1);          

        }

RaiseBill View:
@model IEnumerable<Smart_M.Models.Ipcell> 
@{
    ViewData["Title"] = "IncellTest";
    Layout = "~/Views/Shared/_Layout.cshtml";
} 

<div>
    <div style="padding-left:5%">Usc Code</div>
    <span style="padding-left:5%">@Html.DropDownList("upcList", new SelectList((IEnumerable<Smart_M.Models.ddlupc>)ViewData["dropSrc"], "UscCode", "UscDesc")) </span>
    <br />
</div>

Method 2

DropDownList uses SelectListItem for data binding example:

@Html.DropDownList("Names", new List<SelectListItem>  
{  
   new SelectListItem{ Text="NAME1", Value = "1" },  
   new SelectListItem{ Text="NAME2", Value = "2" },  
   new SelectListItem{ Text="NAME3", Value = "3" },   
})

Please change Model.upcList to List for example:
<div style="padding-left:5%">Usc Code</div>
<span style="padding-left:5%">
@Html.DropDownList("upcList",Model.upcList.Select(x => new SelectListItem(){Text  
 = x.UscDesc ,Value = x.UscCode }).ToList())
</span>

OR
<div style="padding-left:5%">Usc Code</div>
<span style="padding-left:5%">
@Html.DropDownList("upcList", new SelectList(Model.upcList,"UscCode", "UscDesc"))
</span>

Method 3

Binding Dropdown at View

a. We can bind the dropdown list using HTML.Dropdownlist (it is an HTML helper class).

ListItems: <%= Html.DropDownList(“ListItems”)%>

Here we are passing “ListItems” that have been passed from the controller to the view.

b. Another overloaded method of the HTML.DropdownList class for binding a dropdown list.

<%= Html.DropDownList("SelectedItem", (IEnumerable<SelectListItem>)ViewData["ListItems"])%>

Check proper above code then after implement.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x