7
Html.DropDownList拡張メソッドを使用しようとしていますが、列挙でそれを使用する方法を理解できません。ASP.NET MVCの列挙型からドロップダウンリストを作成する方法は?
私のクラス:私の見解では
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; }
}
}
私はすべての国を取得しようとしましたが、私はそれが間違ってやっているように思えます。あなたのビューで
@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 ">
}
使用 '@ Html.EnumDropDownListFor' – Nkosi
http://stackoverflow.com/questions/17280906/binding-an-enum-to-a-dropdownlist-in-mvc-4/17281798 – Graham