Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

asp.net mvc - Binding DropDownList into MVC View

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 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...