Click here to Skip to main content
15,912,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my controller code:

C#
var clentddl= db.ClientDetails.Where(x=>x.IsActive==true).Select(x=>x.Name).ToList();
           ViewBag.Clientname = clentddl.ToList();

           return View();


my view is:
C#
@Html.DropDownListFor(x => x.Clientname , ViewBag.Clientname as List<SelectListItem>, "PleaseSelect", new { @class = "form-control" })



Problem:

i got the data into the list and collected in viewbag. how to bind in view??
Posted

Use below code:

@Html.DropDownListFor(model => model.ClientId, new SelectList(ViewBag.Clientname as System.Collections.IEnumerable, "ClientId","RoleName"), new { id = "ddlClientName" })
 
Share this answer
 
Comments
kesav prakash 4-Dec-14 7:59am    
Thanks...
1.In ASP.NET MVC, all data that have to be used in the view should exist as properties in the used model. So in your case you have to extends your model class and and into it a property that will be used for this drop down list, then you will can use it in the view by using @Html.DropDownListFor() and not to use data cached into the ViewBag.

2.To fix your existing code you have to cache in the ViewBag a list of List<SelectListItem> and not a simple list like you are doing now, so in you controller you have to built this list before to cache it into the view bag.
 
Share this answer
 
v2

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