2016-04-27 4 views
7

मैं 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; } 
    } 
} 

मेरे विचार में मैं सभी देशों पाने की कोशिश की है, लेकिन जैसा मैंने गलत कर रहा हूँ ऐसा लगता है।

@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 "> 
} 
+2

उपयोग '@ Html.EnumDropDownListFor' – Nkosi

+0

http://stackoverflow.com/questions/17280906/binding-an-enum-to-a-dropdownlist-in-mvc-4/17281798 – Graham

उत्तर

8

आपके विचार में आप SelectExtensions.EnumDropDownListFor उपयोग कर सकते हैं:

उदा:

@Html.EnumDropDownListFor(model => model.Countries) 

दिए गए दृश्य की @modelCountries नाम के एक संपत्ति है कि एक enum प्रकार है है।

यदि आप ड्रॉप डाउन में एक डिफ़ॉल्ट टेक्स्ट दिखाना चाहते हैं (जैसे: "देश चुनें")। निम्नलिखित प्रश्न और उत्तर पर एक नज़र डालें।

Html.EnumDropdownListFor: Showing a default text

संबंधित मुद्दे