Source: How to create a drop down list in asp.net mvc4 | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Source: How to create a drop down list in asp.net mvc4

Thursday

Bind data to dropdownlist in Asp.net MVC4:

           This is the basic code for creating a page with a dropdown.

The difference between clasic asp.net web forms and mvc is that mvc doesn't have those events like "selectedIndexChanged", because all the controls are pure html controls.

// in view page model


 public class DropdownViewModel
    {
        public SelectList ProductList { get; set; }
    }

// in controller action 

   public ActionResult Create()
        {
            DropdownViewModel model = new DropdownViewModel();

            List<SelectListItem> listItems = new List<SelectListItem>();
            listItems.Add(new SelectListItem()
            {
                Value = "1",
                Text = "Product 1"
            });
            listItems.Add(new SelectListItem()
            {
                Value = "2",
                Text = "Product 2"
            });
            listItems.Add(new SelectListItem()
            {
                Value = "3",
                Text = "Product 3"
            });
            listItems.Add(new SelectListItem()
            {
                Value = "4",
                Text = "Product 4"
            });
            listItems.Add(new SelectListItem()
            {
                Value = "5",
                Text = "Product 5"
            });
            model.ProductList = new SelectList(listItems, "Value", "Text");

            return View(model);
        }

//And finally this is in view page 

@model YourApplication.Models.DropdownViewModel
 @Html.DropDownList("product", Model.ProductList)

And this is the output of the above code


0 comments:

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets