ASP.NET MVC Dropdown List From SelectList

后端 未结 3 607
北海茫月
北海茫月 2020-11-28 06:02

I am building the following SelectList in my controller.

var u = new NewUser();

u.UserTypeOptions = new SelectList(new List

        
3条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 06:38

    Just try this in razor

    @{
        var selectList = new SelectList(
            new List
            {
                new SelectListItem {Text = "Google", Value = "Google"},
                new SelectListItem {Text = "Other", Value = "Other"},
            }, "Value", "Text");
    }
    

    and then

    @Html.DropDownListFor(m => m.YourFieldName, selectList, "Default label", new { @class = "css-class" })
    

    or

    @Html.DropDownList("ddlDropDownList", selectList, "Default label", new { @class = "css-class" })
    

提交回复
热议问题