Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi to all,
i'm using vs 2013 and mvc


Here i'm bind the value from database to drop-down list without using VIEWDATA.

here i done using view data to bind the database value. Anyother method to bind value from

database to drop-down list.
Posted

You can bind data by writing C# code in your view(cshtml) file with the help of SelectList class.

for example

C#
@using System.Linq
@{    
   using(var uow = new UnitOfWork())
 {
     SelectList labelList = null;
     IEnumerable<label> labels = uow.Repository<label>().GetAll().ToList();

// Suppose you want to display name in DropDown

     var labelsList = labels.OrderBy(l => l.name).ToList();

// You can store current selected dropdown value in Session. Here is Session["labelId"].

     labelList = new SelectList(labelsList, "id", "name", (Session["labelId"] != null) ?   Convert.ToInt32(Session["labelId"]) : 0);

 }
}

// Now you can set labelList to the dropdown as itemsource.

<div>
 @Html.DropDownList("labelsDropDown", labelList as SelectList)
<div>
</div></div>


Hope this will help you .
 
Share this answer
 
v3
Comments
JOTHI KUMAR Member 10918227 24-Jul-14 3:02am    
Thanks for reply but i want using Modal
Punamchand Dhuppad 24-Jul-14 4:29am    
Then pass the Model to your view and use it.
JOTHI KUMAR Member 10918227 24-Jul-14 4:51am    
how can i pass Kindly explain me
Punamchand Dhuppad 24-Jul-14 5:09am    
http://stackoverflow.com/questions/16339398/mvc4-passing-model-from-view-to-controller
or
Search on google "pass data form Controller To View" it will give you lots of example

Now hope it will help you. It just a matter of binding any SelectList value to your dropdown.
@Html.DropDownListFor(m => m.(property list), new SelectList(Model.(property name), "Property ID", "Name"), new { @id = "if u need", @class = "if u need" })


Before this in the viewmodel mention the List<entity> and bind the values from the db in the serverside code. And then use the above code in your view page.

** the code is in Razor syntax.

Thanks
Hope this helps.
 
Share this answer
 

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