我目前在ViewModel的顶部遇到了所有这些混乱,我觉得这违反了DTO的目的。例如,这是我的一个视图模型的构造函数 -
Dictionary<int, string> chargeGroups = new Dictionary<int, string>();
chargeGroups.Add(1, "Administration");
chargeGroups.Add(2, "Annual Leave");
chargeGroups.Add(3, "Bereavement");
chargeGroups.Add(4, "Customer Installation, Setup & Training");
chargeGroups.Add(5, "Customer Support");
chargeGroups.Add(6, "Internal Training & Education");
chargeGroups.Add(7, "Sales & Marketing");
chargeGroups.Add(8, "Sick");
chargeGroups.Add(9, "Software Devel / Maint / Test");
chargeGroups.Add(10, "Software Upgrade / Patch");
chargeGroups.Add(11, "Other");
chargeGroups.Add(12, "Other Absence");
chargeGroups.Add(13, "Warranty");
chargeGroups.Add(14, "Public Holiday");
chargeGroups.Add(15, "Other Paid Leave");
ChargeGroups = new SelectList(chargeGroups, "Key", "Value");
我的观点模型:
[DisplayName("Charge group")]
public short? ChargeGroup { get; set; }
public SelectList ChargeGroups;
然后在我看来:
<div class="editor-label">
@Html.LabelFor(model => model.ChargeGroup)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ChargeGroup, Model.ChargeGroups)
@Html.ValidationMessageFor(model => model.ChargeGroup)
</div>
我应该把这些东西放在哪里?
答案 0 :(得分:5)
当我有一个不会改变的值列表时,我通常使用Enum然后使用自定义Html Helper来渲染一个选择列表。您可以使用元数据描述标记来定制枚举值的显示文本。
这样你就可以使用:
<%: Html.EnumDropDownListFor(model => model.EnuProperty) %>
或
@Html.EnumDropDownListFor(model => model.EnuProperty)
查看Simon的这篇文章,它允许您使用Meta Description属性来定制Enum名称的输出:
How do you create a dropdownlist from an enum in ASP.NET MVC?
这是另一个例子,但缺少元描述属性:
修改强>
你的枚举可能看起来像这样
public enum ChargeGroupEnum
{
[Description("Administration")]
Administration=1,
[Description("Annual Leave")]
AnnualLeave=2,
[Description("Bereavement")]
Bereavement=3,
[Description("Customer Installation, Setup & Training")]
CustomerInstallation=4,
[Description("Customer Support")]
CustomerSupport=5,
[Description("Internal Training & Education")]
InternalTraining=6,
[Description("Sales & Marketing")]
SalesMarketing=7,
[Description("Sick")]
Sick=8,
[Description("Software Devel / Maint / Test")]
Development=9,
[Description("Software Upgrade / Patch")]
Upgrade=10,
[Description("Other")]
Other=11,
[Description("Other Absence")]
OtherAbsence=12,
[Description("Warranty")]
Warranty=13,
[Description("Public Holiday")]
PublicHoliday=14,
[Description(")ther Paid Leave")]
OtherPaidLeave=15
}
然后在您的视图模型上,您可以使用以下内容使字段以无值开始并请求值:
[Required(ErrorMessage=" * required")]
public ChargeGroupEnum? ChargeGroup {get;set;}
然后在您的视图中,您将使用Html Helper“ChargeGroupEnum”,您需要从我链接的帖子中获取。
@Html.EnumDropDownListFor(model => Model.ChargeGroup)
如果您的模型有Int,您可以轻松地从Enum =&gt; Int和Int =&gt;恩纳与铸造。
答案 1 :(得分:0)
我认为在从数据源加载数据并通过辅助方法基于该数据呈现控件之后缓存该数据将是更好的解决方案。
答案 2 :(得分:0)
我决定将选择列表的值保留在另一个名为OtherData的类中,该类位于我的模型文件中的主DBContext类旁边。
答案 3 :(得分:0)
在视图和视图模型中创建SelectList是不是更理想,只是因为视图模型不是紧密耦合到视图对象(SelectList)?
我知道一个视图模型用于一个视图,所以紧密耦合它们更好,但理论上如果视图与不使用SelectList的东西交换,你可以重用更多的视图模型,如果它没有'使用那个SelectList。
答案 4 :(得分:-3)
public static List<string> PaymentCurrency = new List<string> { "USD", "GBP", "EUR", "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK", "DKK", "FJD", "HKD", "HNL", "HUF", "IDR", "ILS", "INR", "ISK", "JPY", "KRW", "LVL", "MXN", "MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR" };
List<SelectListItem> PaymentCurrencyOptionItems = new List<SelectListItem>() { new SelectListItem { Text = "", Value = "" } };
PaymentCurrencyOptionItems.AddRange(Lolio.PaymentCurrency.Select(r => new SelectListItem { Text = r+" "+LangResources.Get("Currency_" + r), Value = r }));
IEnumerable<SelectListItem> LinkPaymentType = new SelectList(PaymentTypeOptionItems, "Value", "Text", lnk.Performance.PaymentType);
Html.DropDownListFor(m => m.PaymentType, LinkPaymentType))