绑定到枚举的下拉列表
Dropdownlist bound to an enum
我正在使用 asp.net mvc。这可能是一个直截了当的。我将我的下拉列表绑定到一个枚举,如下所示
@Html.DropDownListFor(m => m.IndicatorGroups, Model.IndicatorGroups.ToSelectList(), new { @id = "ddlIndicatorGroup" })
模型定义如下
public class SearchControlViewModel
{
...
public GlobalEnums.IndicatorGroup IndicatorGroups { get; set; }
...
}
ToSelectList函数定义如下
public static SelectList ToSelectList<TEnum>(this TEnum enumObj) where TEnum : struct, IComparable, IFormattable, IConvertible
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
select new { Id = Convert.ToInt32(e), Name = e.ToString() };
return new SelectList(values, "Id", "Name", enumObj);
}
现在我添加了带空格的值来枚举,我想显示这些值而不是 "values with underscores"
public enum IndicatorGroup
{
[EnumMember(Value = "Include ANY MatchingIndicator")]
Include_ANY_MatchingIndicator = 1,
[EnumMember(Value = "Include ALL MatchingIndicator")]
Include_ALL_MatchingIndicator,
[EnumMember(Value = "Exclude ANY MatchingIndicator")]
Exclude_ANY_MatchingIndicator,
[EnumMember(Value = "Exclude ALL MatchingIndicator")]
Exclude_ALL_MatchingIndicator
};
我怎样才能做到这一点?
在 MVC 中,我们有 EnumDropDownListFor,我们可以直接与我们的任何枚举绑定,它与 DropDownListFor
非常相似
示例示例:
public enum Courses
{
[Display(Name = "ASP.NET")]
ASPNet,
[Display(Name = "C# .NET")]
CSharp,
[Display(Name = "Java")]
Java,
[Display(Name = "Objective C")]
ObjectiveC,
}
型号:
public class Student
{
[Key]
public String StudentId { get; set; }
[Display(Name="Student Name")]
public String Student { get; set; }
[Display(Name = "Languages")]
public Courses Language { get; set; }
}
在视图中:
<div class="form-group">
@Html.LabelFor(model => model.Languages, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.Languages, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Languages, "", new { @class = "text-danger" })
</div>
</div>
有用linK:http://www.advancesharp.com/blog/1163/mvc-enumdropdownlistfor-bind-with-enum-example
谢谢
卡尔提克
由于我的要求很简单,所以我替换了字符串。我觉得一定有更好的方法来做同样的事情。
public static SelectList ToSelectList<TEnum>(this TEnum enumObj) where TEnum : struct, IComparable, IFormattable, IConvertible
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
select new { Id = Convert.ToInt32(e), Name = e.ToString().Replace('_', ' ') };
return new SelectList(values, "Id", "Name", enumObj);
}
我正在使用 asp.net mvc。这可能是一个直截了当的。我将我的下拉列表绑定到一个枚举,如下所示
@Html.DropDownListFor(m => m.IndicatorGroups, Model.IndicatorGroups.ToSelectList(), new { @id = "ddlIndicatorGroup" })
模型定义如下
public class SearchControlViewModel
{
...
public GlobalEnums.IndicatorGroup IndicatorGroups { get; set; }
...
}
ToSelectList函数定义如下
public static SelectList ToSelectList<TEnum>(this TEnum enumObj) where TEnum : struct, IComparable, IFormattable, IConvertible
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
select new { Id = Convert.ToInt32(e), Name = e.ToString() };
return new SelectList(values, "Id", "Name", enumObj);
}
现在我添加了带空格的值来枚举,我想显示这些值而不是 "values with underscores"
public enum IndicatorGroup
{
[EnumMember(Value = "Include ANY MatchingIndicator")]
Include_ANY_MatchingIndicator = 1,
[EnumMember(Value = "Include ALL MatchingIndicator")]
Include_ALL_MatchingIndicator,
[EnumMember(Value = "Exclude ANY MatchingIndicator")]
Exclude_ANY_MatchingIndicator,
[EnumMember(Value = "Exclude ALL MatchingIndicator")]
Exclude_ALL_MatchingIndicator
};
我怎样才能做到这一点?
在 MVC 中,我们有 EnumDropDownListFor,我们可以直接与我们的任何枚举绑定,它与 DropDownListFor
非常相似示例示例:
public enum Courses
{
[Display(Name = "ASP.NET")]
ASPNet,
[Display(Name = "C# .NET")]
CSharp,
[Display(Name = "Java")]
Java,
[Display(Name = "Objective C")]
ObjectiveC,
}
型号:
public class Student
{
[Key]
public String StudentId { get; set; }
[Display(Name="Student Name")]
public String Student { get; set; }
[Display(Name = "Languages")]
public Courses Language { get; set; }
}
在视图中:
<div class="form-group">
@Html.LabelFor(model => model.Languages, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.Languages, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Languages, "", new { @class = "text-danger" })
</div>
</div>
有用linK:http://www.advancesharp.com/blog/1163/mvc-enumdropdownlistfor-bind-with-enum-example
谢谢
卡尔提克
由于我的要求很简单,所以我替换了字符串。我觉得一定有更好的方法来做同样的事情。
public static SelectList ToSelectList<TEnum>(this TEnum enumObj) where TEnum : struct, IComparable, IFormattable, IConvertible
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
select new { Id = Convert.ToInt32(e), Name = e.ToString().Replace('_', ' ') };
return new SelectList(values, "Id", "Name", enumObj);
}