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
// in controller action
//And finally this is in view page
And this is the output of the above code
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)
@Html.DropDownList("product", Model.ProductList)
And this is the output of the above code
0 comments:
Post a Comment
Share your thoughts here...