bind dropdownlist from database in mvc4
Step2:
Step 1:
First you select dropdownllist data from database using following controller code. here HomeModel is model class . we can add data in list and then pass though viewbag from controller to view
List<HomeModel> lst1 = new List<HomeModel>();
HomeModel hm=new HomeModel();
foreach ( System.Data.DataRow dr1 in dt1.Tables[0].Rows )
{
lst1.Add( new HomeModel { HobbyName = @dr1["HobbyName"].ToString(), HobbyId =int.Parse( @dr1["HobbyId"].ToString()) } );
}
ViewBag.Hobby = lst1;
Step2:
Now you can display viewbag data in in Dropdown using following source code
<div class="editor-field">
@Html.DropDownList( "Designation", ViewBag.Designation as SelectList, "-Select -", new { style = "width:70px;height:25px", @id = "Designation" } )
</div>
Step 3: when your application run page can look like as below
Sign up here with your email
ConversionConversion EmoticonEmoticon