Dynamic DropDownList Binding in ASP.NET MVC With Database

Dynamic DropDownList Binding in ASP.NET MVC With Database

In this article we will learn Dynamic DropDownList Binding in ASP.NET MVC With Database, Here we have bind DropDownList from database using sql server database.

So let’s start to leran Dynamic DropDownList Binding in ASP.NET MVC Step by step

Step 1: Create an MVC Application

Step 2: Add Model class

public class HomeModel
{
public int HobbyId{ get; set; }
public string HobbyName{ get; set; }

}

Step 3: Now add controller and action method for binding DropDownList data for corresponding view or page,so we can pass these binding data to view using viewbag.

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;

Step 4: Now we have bind these viewbag data to DropDownList using below mentioned code in particular page using razor-view syntax.

<div class=”editor-field”>
          @Html.DropDownList( “Designation“, ViewBag.Designation as SelectList, “-Select -“, new { style = “width:70px;height:25px”, @id = “Designation” } )
        </div>

Now, you have try this code and run your web application, and have any query regarding Dynamic DropDownList Binding in ASP.NET MVC With Database, then feel free to comment on comment section

Dynamic DropDownList Binding in ASP.NET MVC With Database

 

SEE MORE

Facebook Notice for EU! You need to login to view and post FB Comments!

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *