Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SelectList objselectlist = new SelectList(TestList, "Id", "CountryName");


here testlist is a list(it has sme value storing inside it ).i want to get that values in objselectlist but its showing null
Posted
Comments
ZurdoDev 13-Jan-16 7:27am    
What is TestList at this point. Is it NULL?
Dj@y 13-Jan-16 8:39am    
put the more code for understanding problem.
Member 11970398 13-Jan-16 8:48am    
public SelectList GetAllprofsssinList()
{
var PList = new List<Profession&();

ds = dropdown.GetDropDownList();
for (int record = 0; record <= ds.Tables[1].Rows.Count - 1; record++)
{

Profession objprof = new Profession();
dynamic row = ds.Tables[1].Rows[record];
objprof.ProfessionId = Convert.ToInt32(row["ID"]);
objprof.ProfessionName = Convert.ToString(row[DescriptionName];
plist.add(objprof );
}

SelectList testList = new SelectList(PList,ProfessionId;, ProfessionName;
return testList;
}

1 solution

Try like this:
C#
public SelectList GetAllprofsssinList()
{
   var PList = new List<professional();>

   ds = dropdown.GetDropDownList();
   List<SelectListItem> lstProfessional = new List<SelectListItem>();

   for (int record = 0; record <= ds.Tables[1].Rows.Count - 1; record++)
   {
      dynamic row = ds.Tables[1].Rows[record];

      // adding records to list of professional
      lstProfessional.Add(
          new SelectListItem  
          {  
             Text = Convert.ToString(row["ID"]), 
	         Value = Convert.ToString(row[DescriptionName]
          });
   }

  // generate SelectList object
  SelectList testList = new SelectList(lstProfessional);

  // returning list
  return testList;
}
 
Share this answer
 
v3
Comments
Member 11970398 14-Jan-16 1:41am    
in lstProfessional.Add(
new SelectListItem
{
Text = Convert.ToInt32(row["ID"]),
Value = Convert.ToString(row[DescriptionName]
});
}
Member 11970398 14-Jan-16 2:02am    
after makeing the change Text = Convert.ToString(row["TestName"]),
Value = Convert.ToString(row["TestID"])
its not givng any error but testList is null
[no name] 14-Jan-16 3:50am    
Make sure that it is adding items to SelectList. Means put a breakpoint near to for loop and see whether it is going inside it. When you say is null, can you check testList.Items is not null.
Member 11970398 18-Jan-16 0:23am    
he i am getting the value there but when i am using that value in listbx in razor view its showing error objectt reference not set
[no name] 18-Jan-16 0:26am    
How are you assigning data to dropdownlist in .cshtml page?

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