我有一个名为 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] 导致为此元素加载 InvoiceType Editor Template。此模板定义为
@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)
}
我的程序中有很多这样的硬编码状态列表。我说硬编码是因为它们不是从数据库中获取的。有没有其他方法可以为下拉菜单创建模板?如何在模型中声明一个枚举并让下拉加载枚举而不必通过视图模型传递它?