Awa*_*Awa 8 asp.net asp.net-mvc
我正在尝试使用Html.DropDownList扩展方法,但无法弄清楚如何将它与枚举一起使用.
我的课程 :
namespace Support_A_Tree.Models
{
public enum Countries
{
Belgium,
Netherlands,
France,
United_Kingdom,
Other
}
[MetadataType(typeof(SupporterMetaData))]
public partial class Person
{
public string Name { get; set; }
public Countries Country { get; set; }
public List<SelectListItem> allCountries()
{
List<SelectListItem> choices = new List<SelectListItem>();
foreach (String c in Enum.GetValues(typeof(Countries)))
{
choices.Add(new SelectListItem() { Text = c , Value = bool.TrueString });
}
return choices;
}
}
public class SupporterMetaData
{
public string Name { get; set; }
[Required]
public Countries Country { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
在我的视图中,我试图获得所有国家,但似乎我做错了.
@using (Html.BeginForm())
{
<div>
<p style = "color: red;">@ViewBag.Message</p>
</div>
<div>
<h2> You want to ... </h2>
<p>Plant trees</p>
@Html.CheckBoxSimple("support", new { @value = "Plant trees" })
<p>Support us financial</p>
@Html.CheckBoxSimple("support", new { @value = "Support financial" })
</div>
<input type="submit" value="Continue ">
}
Run Code Online (Sandbox Code Playgroud)
在您的视图中,您可以使用SelectExtensions.EnumDropDownListFor:
例如:
@Html.EnumDropDownListFor(model => model.Countries)
Run Code Online (Sandbox Code Playgroud)
鉴于@model
该视图的具有指定属性Countries
是一个enum
类型.
如果要在下拉列表中显示默认文本(例如:"选择国家/地区").看看下面的问题和答案.
Html.EnumDropdownListFor:显示默认文本
归档时间: |
|
查看次数: |
9646 次 |
最近记录: |