如何将下面的下拉列表代码修改为多个选择版本?

时间:2015-03-30 12:24:33

标签: c# model-view-controller

我刚刚开始使用Mvc Project,我使用cshtml控制器有点远。作为更多细节,我无法将此下拉列表修改为多个选项,如列表框等。

VIEW:
@Html.DropDownListFor(model => model.UserId, new SelectList(ViewData["User"] as SelectList, "id", "name"), "--Select--", new { @multiple = "multiple", @data_rule_required = "true", @data_msg_required = "Please pick users...", @class = "input-xlarge" })

CONTROLLER:
var userobject = new Dictionary<int, string>();
            for (int i = 0; i < perkull.Count; i++)
            {
                userobject.Add(perkull[i].Key, perkull[i].Value);
            }
            ViewData["User"] = new SelectList(userobject, "Key", "Value");

            ViewBag.User = 0;

            if (!string.IsNullOrEmpty(id))
            {
                int recordid = int.Parse(id);
                var model = db.UserRights.Where(x => x.Id == recordid).SingleOrDefault();
                ViewBag.User = model.UserId;

                return View(model);
            }
            return View();

编辑: 我已经尝试修改listboxfor的代码块,但它没有成功。这是我做的:

@Html.ListBoxFor(model => model.UserId, new MultiSelectList(ViewData["User"] as MultiSelectList, "id", "name"),  new { @multiple = "multiple", @data_rule_required = "true", @data_msg_required = "Please pick users...", @class = "input-xlarge" })

1 个答案:

答案 0 :(得分:0)

尝试

@Html.ListBoxFor(model => model.UserIds, new SelectList(ViewData["User"] as SelectList, "id", "name"),  new { @multiple = "multiple", @data_rule_required = "true", @data_msg_required = "Please pick users...", @class = "input-xlarge" })

请注意,我使用了UserIds

List<UserId>