You must Sign In to post a response.
  • Category: ASP.Net MVC

    How to bind MVC dropdown list

    am new to mvc frame work

    i need an help to work with dropdownlist please help me
  • #768339
    Hi,

    Implementing Binding on Dropdownlist is very simpler.
    For making it happen, we need prepare the codes in the following places.

    Initially we need to create a model.

    public class DsrModel
    {
    //Fill the coulmns

    }



    Now create the Controller page for Dropdownlist.

    public class BindingController:Controller
    {

    public ActionResult BindingDD()
    {
    List<selectedListItem> lst = new List<selectedListItem>();

    // We can fill this list by adding the values from the list or we can get the values from Database.

    }

    }



    Now coming to view. we need to DropDownlist in the view page which to be stronglytyped one.
    Then we can call the HTML to map the values.


    <%= Html.DropDownList("SelectedItem", Model.ListItems,"-----Select----") %>

    Thanks,
    Mani

  • #768343
    You need to use IEnumerable<SelectListItem> in your program to bind MVC dropdown list
     @Html.DropDownList("SelectedStoreType", (IEnumerable<SelectListItem>)ViewBag.StoreType)
    Code for control action
     public ActionResult SelectStore()
    {
    List<SelectListItem> items = new List<SelectListItem>();
    items.Add(new SelectListItem { Text = "MyStore1", Value = "MyStore1", Selected=true });
    items.Add(new SelectListItem { Text = "MyStore2", Value = "MyStore2" });
    var model = new MyViewModel
    {
    MovieTypes = items
    };
    return View(model);
    }


    Useful reference :
    http://dotnetmentors.com/mvc/how-to-bind-dropdownlist-in-asp-net-mvc-application.aspx
    http://stackoverflow.com/questions/12985088/binding-dropdownlist-into-mvc-view


  • Sign In to post your comments