Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding DropDownList into MVC View

Tags:

asp.net-mvc

Reading from here: ASP.NET MVC

Action SelectCategory has been created inside controller -

 public ActionResult SelectCategory() {

     List<SelectListItem> items = new List<SelectListItem>();    
     items.Add(new SelectListItem { Text = "Action", Value = "0"});    
     items.Add(new SelectListItem { Text = "Drama", Value = "1" });    
     items.Add(new SelectListItem { Text = "Comedy", Value = "2", Selected = true });    

     ViewBag.MovieType = items;

     return View();    
 }

I am not able to understand binding of Data in following line.

@Html.DropDownList("MovieType")

While binding data in similar way,

@Html.DropDownList("IdList");

I obtain following error-

There is no ViewData item of type 'IEnumerable' that has the key 'IdList'.

Controller Action:

public ActionResult SelectId()
    {
        List<SelectListItem> items = new List<SelectListItem>();
        items.Add(new SelectListItem { Text = "MyId1", Value = "MyId1", Selected=true });
        items.Add(new SelectListItem { Text = "MyId2", Value = "MyId2" });

        ViewBag.IdList = items;
        return View();
    }

What am I missing ? Thank you for your help !

like image 833
Abhijeet Avatar asked Oct 20 '12 03:10

Abhijeet


2 Answers

You have set ViewBag.MovieType => when you use @Html.DropDownList("MovieType") the dropdown will use this value. When you write @Html.DropDownList("IdList"), the helper doesn't find a corresponding IdList property in ViewBag and throws an error because it doesn't know from where to bind the data.

Alternatively if you want to change the name of the dropdown you could use the following:

@Html.DropDownList("SelectedMovieType", (IEnumerable<SelectListItem>)ViewBag.MovieType)

and your POST action will have a SelectedMovieType parameter to retrieve the selected value.

But I would avoid ViewBag. Defining a view model is better:

public class MyViewModel
{
    public string SelectedMovieType { get; set; }
    public IEnumerable<SelectListItem> MovieTypes { get; set; }
}

and then have your controller action populate this view model and pass it to the view:

public ActionResult SelectId()
{
    List<SelectListItem> items = new List<SelectListItem>();
    items.Add(new SelectListItem { Text = "MyId1", Value = "MyId1", Selected=true });
    items.Add(new SelectListItem { Text = "MyId2", Value = "MyId2" });

    var model = new MyViewModel
    {
        MovieTypes = items    
    };

    return View(model);
}

and in your strongly typed view:

@model MyViewModel
@Html.DropDownListFor(x => x.SelectedMovieType, Model.MovieTypes)
like image 83
Darin Dimitrov Avatar answered Sep 27 '22 02:09

Darin Dimitrov


while binding you list to DropDown you need to type cast it to IEnumerable as you use @Html.DropDown control which is not strongly type with Model

In View

@Html.DropDownList("MovieType",(IEnumerable<SelectListItem>)ViewBag.MovieType) 

and for another way if you bind IdList then

@Html.DropDownList("IdList",(IEnumerable<SelectListItem>)ViewBag.IdList)
like image 34
Shivkumar Avatar answered Sep 24 '22 02:09

Shivkumar