Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi to all,
Here i want to bind in drop-down from database using modal.But i bind the value using

viewdata. so kindly help how to bind drop-down from database using modals.



DESCRIPTION:-
in controller
XML
List<SelectListItem> list = new List<SelectListItem>();
               list.Add(new SelectListItem { Text = "Select Country", Value = "Select Country" });//set default text for DropdownList
               var cat = (from c in db.Country select c).ToArray();//get data from database using linq
               for (int i = 0; i < cat.Length; i++)
               {
                   list.Add(new SelectListItem { Text = cat[i].Country_Name, Value = cat[i].Country_Cde.ToString() });
               }
               ViewData["ViewCountry"] = list;


in view:-
@Html.DropDownList("Country_Cde", (IEnumerable<SelectListItem>)ViewData["ViewCountry"], new { id = "Country_Cde", @class = "form-control control-text" })

Kindly suggest me how to bind values in drop-down from database using modals.
Posted
Updated 25-Jul-14 21:34pm
v2
Comments
[no name] 26-Jul-14 1:25am    
u can directlly bind object to dropdownlist datasource property of dropdownlist
JOTHI KUMAR Member 10918227 26-Jul-14 2:14am    
thanks for reply me can you explain me in code bcz i applied some modal but i got error NullReferenceException Class
JOTHI KUMAR Member 10918227 26-Jul-14 5:35am    
Kindly reply me sir

I would recommend you to go through the links below:
Creating a DropDownList for Enums in ASP.NET MVC[^]
How to simple Html.DropDownListFor MVC.NET[^]

    --Amy
 
Share this answer
 
Comments
JOTHI KUMAR Member 10918227 26-Jul-14 9:18am    
its not working :( Kindly help me please
JOTHI KUMAR Member 10918227 28-Jul-14 0:41am    
if i using model it get error nullreference exception
Model :
List<SelectListItem> ListItem = new List<SelectListItem>();


ListItem = from itm in tabledata
select new SelectList
{
Value = itm.Value,
Text = itm.Name
}).ToList();


OR

ListItem.Add(new SelectListItem { Value = "true", Text = "Yes" });
ListItem.Add(new SelectListItem { Value = "false", Text = "No" });

@html.DropDownListFor(m=>m.Item, ListItem,new{});
 
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