如何确保selectlist项属于同一个SelectListGroup?

时间:2016-09-29 11:28:28

标签: c# asp.net-mvc-5 selectlist optgroup selectlistitem

我正在尝试使用<optgroup>元素,并尝试将多个项目归属于同一 optgroup

我的代码:

    public async Task<ActionResult> GetStatsView()
    {
        var users = await _administrationServiceAPI.GetPersons();
        var tanks = await _administrationServiceAPI.GetTanks();


        var list = new List<SelectListItem>();

        foreach (var u in users.Payload)
        {
            SelectListItem item = new SelectListItem {Text = u.Name, Value = u.Id.ToString(), Group = new SelectListGroup {Name = "Users"} };
            list.Add(item);
        }

        foreach (var u in tanks.Payload)
        {
            SelectListItem item = new SelectListItem { Text = u.Name, Value = u.Id.ToString(), Group = new SelectListGroup { Name = "Tanks" } };
            list.Add(item);
        }

        @ViewBag.MyList = list;

        return View("StatsView");
    }

视图:

@Html.DropDownList("MyList")

结果:

enter image description here

Desiered结果是:

用户

用户名

用户名

用户名

我该怎么做?

1 个答案:

答案 0 :(得分:2)

要使其有效,您需要重复使用// .extend adds a .withShutdown prototype method to the Server object require('http-shutdown').extend(); var server = require('http').createServer(function(req, res) { res.end('God job!'); }).withShutdown(); // <-- Easy to chain. Returns the Server object // Sometime later, shutdown the server. server.shutdown(function() { console.log('Everything is cleanly shutdown.'); }); 。只需使用它:

SelectListGroup

当然坦克也是如此。

名称相同并不重要,所使用的实例在各个项目中必须相同才能将它们分组为单个 var usersGroup = new SelectListGroup {Name = "Users"}; foreach (var u in users.Payload) { SelectListItem item = new SelectListItem {Text = u.Name, Value = u.Id.ToString(), Group = usersGroup }; list.Add(item); }