简体   繁体   English

C#MVC 5 Razor:Dropdownlist显示小写值而不是大写

[英]C# MVC 5 Razor: Dropdownlist displaying the lower-case value instead of upper-case

I have an enum which looks like this (the descriptions are censored but the values are accurate): 我有一个看起来像这样的枚举(描述是经过审查的,但值是准确的):

    public enum internalTypes
    {
        [Description("Description 1 (W)")]
        W,
        [Description("Description 2 (w)")]
        w,
        [Description("Description 3 (A)")]
        A,
        [Description("Description 4 (C)")]
        C,
        [Description("Description 5 (K)")]
        K,
        [Description("Description 6 (X)")]
        X
    }

I have to display this dropdownlist in a view which gets its data from the database. 我必须在从数据库获取其数据的视图中显示此下拉列表。 For this problem, please note that the value "W" (uppercase) is the one I want to display. 对于此问题,请注意,值“ W”(大写)是我要显示的值。

The DropDownList code looks like this: DropDownList代码如下所示:

    @Html.DropDownListFor(model => model.Internal_List[i].ValueToDisplay, new SelectList(Model.listOfTypes, "Value", "Text", Model.Internal_List[i].ValueToDisplay), "", new { @class = "full", style = "width:275px" })

Model.listOfTypes: Model.listOfTypes:

        IEnumerable<ExtensionMethods.internalTypes> internalTypes= Enum.GetValues(typeof(ExtensionMethods.internalTypes)).Cast<ExtensionMethods.internalTypes>();
        evm.listOfTypes        = from inter in internalTypes
                                 select new SelectListItem
                                 {
                                     Text = ExtensionMethods.GetDescription(inter),
                                     Value = inter.ToString()
                                 };

Where Internal_List[i].ValueToDisplay is indeed sent correctly - that is, it correctly sends "W" (uppercase). 实际上正确发送了Internal_List [i] .ValueToDisplay的地方-也就是说,它正确地发送了“ W”(大写)。 However, the view displays "w" (lowercase): 但是,视图显示“ w”(小写):

在此处输入图片说明

This is a problem, because I must display "W", not "w", because that is the value that is stored in the database. 这是一个问题,因为我必须显示“ W”而不是“ w”,因为那是存储在数据库中的值。 Similarily, if "w" was stored, then I'd have to display "w", not "W". 同样,如果存储了“ w”,则我必须显示“ w”,而不是“ W”。 I don't understand why "W" is converted to "w" in this case, as when I debug my program, I can clearly see that the value being passed to the view is "W". 我不明白为什么在这种情况下将“ W”转换为“ w”,因为在调试程序时,我可以清楚地看到传递给视图的值为“ W”。

Would anyone be able to tell me where I may have made a mistake? 谁能告诉我我可能在哪里犯了错误? Please note that I can't simply give "w" another value - I don't have a choice in the matter. 请注意,我不能简单地给“ w”另一个值-我对此没有选择。

You cannot select the correct option using the DropDownListFor() (or the EnumDropDownListFor() etc. method). 您不能使用DropDownListFor() (或EnumDropDownListFor()等方法)选择正确的选项。 Internally the methods generate a new IEnumerable<SelectListItem> in order to set the Selected attribute based on the value of the property your binding to. 在内部,这些方法生成一个新的IEnumerable<SelectListItem> ,以便根据绑定到的属性的值来设置Selected属性。

Specifically the private static IEnumerable<SelectListItem> GetSelectListWithDefaultValue() method (you can view the source code here ) compares the values using StringComparer.OrdinalIgnoreCase so a value of w (lowercase) and W (uppercase) both match and both options would be set with the selected="selected" attribute. 具体来说, private static IEnumerable<SelectListItem> GetSelectListWithDefaultValue()方法(您可以在此处查看源代码 )使用StringComparer.OrdinalIgnoreCase比较值,因此w (小写)和W (大写)的值都匹配,并且两个选项都可以通过selected="selected"属性。

But because your generating a single <select> (without the multiple attribute), that would be invalid (only one option can be selected), so the browser selects only the last one that is rendered with selected="selected" 但是,因为您生成的是一个<select> (不包含multiple属性),那将是无效的(只能选择一个选项),因此浏览器仅选择使用selected="selected"呈现的最后一个。

If you cannot change the values of your enum , and want to use a HtmlHelper method, you would need to write your own extension method (copy the source code into your own project, delete the various overloads you do not nee,d and make the modification so that its not case sensitive) 如果您无法更改enum的值,并且想要使用HtmlHelper方法,则需要编写自己的扩展方法(将源代码复制到您自己的项目中,删除您不需要的各种重载,并使修改,使其不区分大小写)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM