【问题标题】:Radio Button List, Check Boxes, Drop Down Lists and Enums/Dictionary Lists单选按钮列表、复选框、下拉列表和枚举/字典列表
【发布时间】:2012-03-01 14:44:50
【问题描述】:

那里的信息量(以及这里的 SO)不是低估它,而是 . . .压倒性的。

在 MVC 3 中使用枚举创建单选按钮列表、复选框和下拉列表的示例有很多。我的问题(可能答案是“你不能那样做”)是我希望我的代码保持一致,同时不使用扩展(大多数示例都有不同类型的扩展)。

足以让我的头爆炸。

所以,虽然它可能是重复的,虽然帖子可能会被关闭,但我想问是否有一组优雅、简单且可能通用的 MVC3 控件用于单选按钮、复选框和下拉菜单?

我想要完成的事情:

单选按钮 我将这些用于“是/否”以及更长的答案(想想:完整的句子),a)在 DropDownList 中不适合或看起来不合适,b)作为用户可以考虑的选择看起来更好。我已经使用下面的代码并取得了很好的效果,但如果可能的话,我不知道如何将它用于复选框或下拉菜单。

复选框 “多个”选项的明显选择,但仅限于应显示的内容。

下拉菜单 非常适合 0-5 个答案,如选择:“0(无)、1、2、3、4、5 或更多”,或简单的 1-3 个单词短语(否则,我会使用单选按钮)。

我的问题: 我不是程序员。我当然可以摆脱我已经盲目接受的范式(见下文)(因为它一直在为我的单选按钮列表工作),但我想要一些可重用的东西。对我来说,“可重复使用”有时意味着类似。

我的范式(或我认为类似的范式,希望我可以在复选框或下拉菜单中使用): 我正在为我的单选按钮使用枚举/字典列表,如下所示:

public SomethingList Something{ get; set; }
        public enum SomethingList
        {
            Something_Something1,
            Something_Something2
        }
        public class SomethingDictionary
        {
            public static readonly Dictionary<SomethingList , string> nameDictionary = new Dictionary<SomethingList , string>
            {
            { SomethingList.Something_Something1, "Whatever text I want, whether it's Yes/No for a check box, a simple 1-2 word phrase for dropdowns, or long sentences as I use in my radiobutton lists." },
            { SomethingList.Something_Something2, "Whatever OTHER text I want, whether it's Yes/No for a check box, a simple 1-2 word phrase for dropdowns, or long sentences as I use in my radiobutton lists." },
             };
            static string ConvertSomething(SomethingListnamelist)
            {
                string name;
                return (somethingDictionary.TryGetValue(somethinglist, out name))
                    ? name : somethinglist.ToString();
            }
            static void Main()
            {
                Console.WriteLine(ConvertSomething(SomethingList.Something_Something1));
                Console.WriteLine(ConvertSomething(SomethingList.Something_Something2));
            }
        }

有什么建议/建议吗? (或者,我可以通过复选框/下拉菜单继续我的范例吗?)

【问题讨论】:

    标签: asp.net-mvc-3 checkbox enums drop-down-menu radio-button


    【解决方案1】:

    尝试使用MVCGrabBag。它具有所有输入的通用代码库。它使用枚举和一些不同的风格来让它工作。但是,一旦您了解了基础知识,您就不会失望。你要做的就是做一个@Html.FullFieldEditor,它会用代码来处理其余的事情。基本上有一个 Selector.cs 和其他一些文件,就像 @Html.EditorFor 帮助器所做的那样,可以确定要创建的输入。而不是使用一堆 /EditorTemplates/ 它只是有一个可以处理所有事情。

    它似乎是相当新的,所以我相信它会获得动力。对于我们这些非超人的编码人员,我不得不想象,这将缓解自网络诞生以来一直存在的东西(我说的是输入)没有任何统一结构的问题。

    祝你好运。

    【讨论】:

    • 谢谢!对于我需要的东西,这看起来很有希望。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 2011-07-03
    • 2010-10-16
    • 1970-01-01
    相关资源
    最近更新 更多