Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i have a html table having a column. i added dropdownlistfor in this column like below

@Html.DropDownListFor(model => item.CategoryID, (IEnumerable<SelectListItem>)ViewData["categories"], new { @id = "category", DataValueField = item.CategoryID, DataTextField = item.CategoryName, SelectedValue = item.CategoryName })

but when retrieving data from the table and bind to this table, this dropdown list not showing the correct value

can anybody help me..

thanks
Kunjammu
Posted
Comments
Snesh Prajapati 11-Apr-14 7:02am    
"dropdown list not showing the correct value" I am not getting. are you getting the values or not.
Kunjammu 11-Apr-14 7:14am    
no..

1 solution

Try this. In my case it is working fine.
@Html.DropDownList("SelectedEmployee",   new SelectList((System.Collections.IEnumerable)ViewData["categories"], "CategoryId", "CategoryName"))


Here CategoryId and CategoryName is the property of your Category model.
 
Share this answer
 
Comments
Kunjammu 11-Apr-14 7:21am    
DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'CategoryID'.

this error showing
Snesh Prajapati 11-Apr-14 7:25am    
did you created model?
Snesh Prajapati 11-Apr-14 7:26am    
write your controller action code that can help me to find out the problem.
Kunjammu 11-Apr-14 7:33am    
public ActionResult ViewWorkStatus()
{
WorkStatusController work = new WorkStatusController();
DataTable dt = work.GetWorkStatus();
CategoryBLL bllObj = new CategoryBLL();
DataTable dt1 = bllObj.GetCategory();
SelectList selectList = new SelectList((IEnumerable<category>)ConvertCategoryToList(dt1), "CategoryID", "CategoryName");
ViewData["categories"] = selectList;

return View(ConvertToList(dt));
}

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