我一直致力于在WebGrid中绑定各种学生数据。数据包含枚举值,因此在绑定Webgrid后我获得输出值但我想为该值显示相应的枚举文本。 下面是我在MVCDemo.Common.Enum命名空间中的Enum
public enum Gender
{
[Display(Name = "Male")]
Male = 1,
[Display(Name = "Female")]
Female = 2,
[Display(Name = "Other")]
Other = 3
}
这是我的WebGrid
var AdminGrid = new WebGrid(source: lstStudent, canPage: true, rowsPerPage: 5, canSort: true, selectionFieldName: "selectedRow");
@AdminGrid.GetHtml(
tableStyle: "table table-bordered table-hover table-condensed",
headerStyle: "head",
alternatingRowStyle: "altRow",
selectedRowStyle: "selectRow",
mode: WebGridPagerModes.All,
rowStyle: "webGridDataRow",
columns: AdminGrid.Columns(
AdminGrid.Column("Firstname", "First Name"),
AdminGrid.Column("Email", "Email"),
AdminGrid.Column("Gender", "Gender"),
AdminGrid.Column("IdRole", "Role")))
我的问题是性别属于枚举类型,但在输出中它显示1,2或3,但我想根据值显示男性,女性,其他。
答案 0 :(得分:0)
grid.Column("Gender", "Gender",format: (item) => item.Gender == 1 ?
Enum.Gender.Male : item.Gender == 2 ? Enum.Gender.Female : Enum.Gender.Other),
这是
1 is Male,
2 is Female,
3 is Other
尝试它将100%工作。