gpt4 book ai didi

c# - ASP.NET MVC 在哪里保存 SelectLists 的值

转载 作者:太空宇宙 更新时间:2023-11-03 11:27:51 25 4
gpt4 key购买 nike

我目前在我的 ViewModels 的顶部遇到了所有这些困惑,我觉得这违反了 DTO 的目的。例如,这是在我的一个 View 模型的构造函数中 -

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

我的 View 模型:

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

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

最佳答案

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

这样你就可以使用:

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

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

查看 Simon 的这篇文章,它允许您使用元描述属性来自定义枚举名称的输出:

How do you create a dropdownlist from an enum in 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
}

然后在您的 View Model 上,您可以使用以下内容使该字段以无值开头并需要一个值:

 [Required(ErrorMessage=" * required")]
public ChargeGroupEnum? ChargeGroup {get;set;}

然后在您看来,您将使用 Html Helper“ChargeGroupEnum”,您需要从我链接到的帖子中获取它。

 @Html.EnumDropDownListFor(model => Model.ChargeGroup) 

如果您的模型有一个 Int,您可以轻松地从 Enum => Int 和 Int => Enum 转换。

关于c# - ASP.NET MVC 在哪里保存 SelectLists 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8825398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com