如何在下拉列表MVC5中为每个dropdownitem设置标题

时间:2016-12-21 15:18:07

标签: c# jquery asp.net-mvc

控制器

p

查看:

public void setViewBags()
{
    List<GroepModel> groepen = Mapper.Map<List<GroepenWerkvorm>, List<GroepModel>>(db.GroepenWerkvorms.ToList());

    var groepmodel = new DropDownModel();
    groepmodel.list = new SelectList(groepen, "id", "Naam");
    ViewBag.groepmodel = groepmodel;
}

@Html.DropDownListFor(model => model.selectedItem, ViewBag.groepmodel.list as IEnumerable<SelectListItem>,"Selecteer een groep", new { @class = "groepen" }) 中的每个元素都有一个名为description的属性。我想在下拉列表中将每个元素的标题设置为相应的描述,因此用户可以将鼠标悬停在它们上方以查看说明。

我假设我可能需要JQuery才能这样做?

groepen

我猜这可能有用,但我如何得到每个元素的正确描述?

1 个答案:

答案 0 :(得分:2)

DropDownListFor帮助程序不支持在生成的option元素上设置其他属性。如果您需要设置title属性,则需要手动生成选项:

<select id="@Html.IdFor(m => m.selectedItem)" name="@Html.NameFor(m => m.selectedItem)">
    @foreach (var groepModel in groepen)
    {
        <option value="@groepModel.id" title="@groepModel.Description">@groepModel.Naam</option>
    }
</select>
相关问题