假设我们有一个Person类,其中包含Name和IsMale属性:

public class Person
{
    public string Name { get; set; }
    public bool IsMale { get; set; }
}

我们可以使用Lambda表达式来创建一个选项列表,其中包含男性和女性选项:

List<SelectListItem> genderOptions = new List<SelectListItem>
{
    new SelectListItem { Text = "男", Value = "true" },
    new SelectListItem { Text = "女", Value = "false" }
};

然后在视图中使用Html.DropDownListFor来显示选项:

@model Person

@Html.LabelFor(model => model.IsMale)
@Html.DropDownListFor(model => model.IsMale, genderOptions)
用aspnet webmvc拉姆达表达式编写一个男女选项

原文地址: https://www.cveoy.top/t/topic/blxk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录