1

データ注釈用に拡張しているInvoiceというエンティティがあります

   [MetadataType(typeof(InvoiceMetadata))]
    public partial class Invoice
    {
        // Note this class has nothing in it.  It's just here to add the class-level attribute.
    }

    public class InvoiceMetadata
    {
        // Name the field the same as EF named the property - "FirstName" for example.
        // Also, the type needs to match.  Basically just redeclare it.
        // Note that this is a field.  I think it can be a property too, but fields definitely should work.

        [HiddenInput]
        public int ID { get; set; }

        [Required]
        [UIHint("InvoiceType")]
        [Display(Name = "Invoice Type")]
        public string Status { get; set; }


        [DisplayFormat(NullDisplayText = "(null value)")]
        public Supplier Supplier { get; set; }

    }

Uhint [InvoiceType]により、この要素のInvoiceTypeEditorテンプレートがロードされます。このテンプレートは次のように定義されます。

@model System.String

@{
      IDictionary<string, string> myDictionary = new Dictionary<string, string> { 
                                                                                        { "N", "New" }
                                                                                        , { "A", "Approved" },
                                                                                        {"H","On Hold"}
                                                                                        };

      SelectList projecttypes= new SelectList(myDictionary,"Key","Value");

       @Html.DropDownListFor(model=>model,projecttypes)     

 }

私のプログラムにはそのようなハードコードされたステータスリストがたくさんあります。データベースからフェッチされないため、ハードコードされていると言います。ドロップダウン用のテンプレートを作成する他の方法はありますか?モデルで列挙型を宣言し、ビューモデルを通過せずにドロップダウンで列挙型をロードするにはどうすればよいですか?

4

1 に答える 1