DropDownList在MVC5中不起作用

时间:2015-03-17 09:21:09

标签: c# asp.net-mvc asp.net-mvc-5

<div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Title", new SelectList(Enum.GetValues(typeof(OnLineShoppingCart.Models.Supplier.Salutation))), "Select The Salutation", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>

1 个答案:

答案 0 :(得分:2)

试试这个:

<强>控制器

        IEnumerable<SelectListItem> TitleList = null;
        TitleList = (from m in Enum.GetValues(typeof(Titles)).Cast<Titles>() select m).AsEnumerable().Select(m => new SelectListItem() { Text = m.ToString(), Value = m.ToString() });
        ViewBag.TitleList = new SelectList(TitleList, "Value", "Text");

查看

            <div class="form-group">
                @Html.LabelFor(m => m.Title, new { @class = "col-md-2 control-label" })
                <div class="col-md-6">
                    @Html.DropDownListFor(m => m.Title, new SelectList(ViewBag.TitleList, "Value", "Text"), "--Select Title--", new { @class = "form-control", placeholder = "Title"})<br />
                    @Html.ValidationMessageFor(m => m.Title, null, new { @class = "text-danger" })
                </div>
            </div>