将DropDownList绑定到MVC视图 [英] Binding DropDownList into MVC View

查看:100
本文介绍了将DropDownList绑定到MVC视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从此处阅读:动作SelectCategory已在控制器内部创建-

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");

我收到以下错误-

没有类型为"IEnumerable"的ViewData项 拥有键"IdList".

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

控制器操作:

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 !

推荐答案

使用@Html.DropDownList("MovieType")时已设置ViewBag.MovieType =>,则下拉列表将使用此值.编写@Html.DropDownList("IdList")时,帮助程序在ViewBag中找不到相应的IdList属性,并因为不知道从何处绑定数据而引发错误.

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)

,并且您的POST操作将具有一个SelectedMovieType参数来检索所选值.

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

但是我会避免使用ViewBag.定义视图模型更好:

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)

这篇关于将DropDownList绑定到MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆