如何显示来自Controller的简单列表以在View的select元素中查看?

时间:2018-10-19 08:54:56

标签: c# html asp.net-mvc razor

每当我尝试以下代码时,我的应用程序似乎都能正常运行:

查看:Index.cshtml

@{
    List<SelectListItem> listItems = new List<SelectListItem>();
    listItems.Add(new SelectListItem
    {
        Text = "Exemplo1",
        Value = "Exemplo1"
    });
    listItems.Add(new SelectListItem
    {
        Text = "Exemplo2",
        Value = "Exemplo2",
        Selected = true
    });
    listItems.Add(new SelectListItem
    {
        Text = "Exemplo3",
        Value = "Exemplo3"
    });
}

@Html.DropDownList("name", listItems)

但是我不希望在View中发生这样的列表的任何逻辑。相反,我希望控制器来处理这个问题:

控制器:SelectionController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ExampleMVC.Controllers
{
    public class SelectionController : Controller
    {
        // GET: Selection
        public ActionResult Index()
        {
            //For first dropdown:
            List<SelectListItem> optionList1 = new List<SelectListItem>();
            optionList1.Add(new SelectListItem
            {
                Text = "Option 1",
                Value = "Option 1"
            });
            optionList1.Add(new SelectListItem
            {
                Text = "Option 2",
                Value = "Option 2"
            });
            optionList1.Add(new SelectListItem
            {
                Text = "Option 3",
                Value = "Option 3"
            });

            //For second dropdown:
            List<SelectListItem> optionList2 = new List<SelectListItem>();
            optionList1.Add(new SelectListItem
            {
                Text = "Option 1",
                Value = "Option 1"
            });
            optionList1.Add(new SelectListItem
            {
                Text = "Option 2",
                Value = "Option 2"
            });
            optionList1.Add(new SelectListItem
            {
                Text = "Option 3",
                Value = "Option 3"
            });


            List<SelectList> optionList3 = new List<SelectList>();
            optionList1.Add(new SelectListItem
            {
                Text = "Option 4",
                Value = "Option 4"
            });
            optionList1.Add(new SelectListItem
            {
                Text = "Option 5",
                Value = "Option 5"
            });
            optionList1.Add(new SelectListItem
            {
                Text = "Option 6",
                Value = "Option 6"
            });

            List<SelectListItem> listItems = new List<SelectListItem>();
            listItems.Add(new SelectListItem
            {
                Text = "Exemplo1",
                Value = "Exemplo1"
            });
            listItems.Add(new SelectListItem
            {
                Text = "Exemplo2",
                Value = "Exemplo2",
                Selected = true
            });
            listItems.Add(new SelectListItem
            {
                Text = "Exemplo3",
                Value = "Exemplo3"
            });
            ViewBag.listItems = listItems;

            ViewBag.optionList1 = optionList1;
            ViewBag.optionList2 = optionList2;
            ViewBag.optionList3 = optionList3;
            return View();
        }
    }
}

例如,每当我尝试在视图中调用ViewBag.listItems时,

@Html.DropDownList("name", ViewBag.listItems)

这将返回错误:

“ HtmlHelper”没有名为“ DropDownList”的适用方法,但似乎具有该名称的扩展方法。扩展方法不能动态调度。考虑强制转换动态参数或在不使用扩展方法语法的情况下调用扩展方法。

我该如何解决这个问题?

修改:

使用:@Html.DropDownList("name", (SelectList) ViewBag.listItems)

将导致:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法将System.Collections.Generic.List的类型转换为System.Web.Mvc.SelectList

1 个答案:

答案 0 :(得分:1)

您可以将ViewBag.Item传递到下拉列表中。

@Html.DropDownList("name", new SelectList(ViewBag.listItems, "Value", "Text"))

OR

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