ASP.NET MVC SelectLists的值保存位置

本文关键字:保存 位置 SelectLists NET MVC ASP | 更新日期: 2023-09-27 18:19:58

我目前在ViewModels的顶部有所有这些混乱,我觉得这违反了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>

我应该把这些东西放在哪里?

ASP.NET MVC SelectLists的值保存位置

当我有一个不会更改的值列表时,我通常使用Enum,然后使用自定义Html Helper来呈现选择列表。您可以使用元数据描述标记自定义枚举值的显示文本。

这样你就可以使用:

 <%: Html.EnumDropDownListFor(model => model.EnuProperty) %>

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

查看Simon的这篇文章,它允许您使用Meta-Description属性自定义Enum名称的输出:

如何在ASP.NET MVC中从枚举创建下拉列表?

这里是另一个例子,但它缺少元描述属性:

http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net-mvc-creating-a-dropdownlist-helper-for-enums.aspx

编辑

你的枚举可能看起来像这个

 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=>Int和Int=>Enum进行强制转换。

我认为在从数据源加载数据后缓存数据,并通过helper方法呈现基于该数据的控件将是更好的解决方案。

我决定将选择列表的值保留在另一个名为OtherData的类上,该类位于我的模型文件中的主DBContext类旁边。

与视图模型相比,在视图中创建SelectList不是更理想吗?这样视图模型就不会与视图对象(SelectList)紧密耦合吗?

我知道视图模型是为视图设计的,所以紧密耦合它们更合适,但理论上,如果视图与不使用SelectList的视图交换,如果它不使用SelectList,你可以重用更多的视图模型。

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))