1
我有稱爲發票,我延伸,用於數據註解的實體DropDownList的和EditorTemplates
[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編輯模板此element.This模板被定義爲
@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)
}
我在我的程序中有很多這樣的硬編碼狀態列表。我說硬編碼是因爲它們沒有從數據庫中獲取。有沒有其他方法可以爲下拉菜單創建模板?我該如何在模型中聲明一個枚舉,並讓下拉列表加載枚舉而不必將其傳遞給視圖模型?
如果我要創建一個HTML擴展方法,我應該在模型中聲明狀態類?如果是的話,我必須從HTMLextension(視圖)訪問模型是不是違反規則? – superartsy
這是您的項目將受益於創建「ViewModels」的地方:http://stackoverflow.com/questions/664205/viewmodel-best-practices 在ViewModel中,您可以在其中爲您的狀態創建一個屬性。 – Jesse
我明白ViewModel應該具有作爲屬性的狀態。但是你在哪裏聲明狀態類 - 在模型中?如果是這樣的話 - 擴展方法訪問該類嗎? – superartsy