在那里(和在这里)的信息量是,不要低估它,但。 。 。压倒。单选按钮列表,复选框,下拉列表和枚举/字典列表
有很多关于如何使用枚举在MVC 3中创建单选按钮列表,复选框和下拉列表的示例。我的问题(也许答案是“你不能这样做”)是我希望在我的代码中保持一致,不要使用扩展名(并且大多数示例都有不同类型的扩展名)。
这足以让我的头部爆炸。因此,虽然它可能是重复性的,并且虽然帖子可能会被关闭,但我会问是否有一个优雅,简单,可能通用的单选按钮,复选框和下拉列表的MVC3控件集?
我试图做到:
单选框我使用这些为“是/否”,以及更长的答案(认为:完整的句子) 一个)将不适合或在DropDownList中看起来很合适,并且b)作为用户可以考虑的选择,看起来更好。我已经在下面的代码中使用了很好的结果,但是如果可能的话,不知道如何将它用于复选框或下拉菜单。
复选框 “多于一个”选项的明显选择,但仅限于应显示的内容。
DROPDOWNS 大0-5的答案,如在选项: “0(无),1,2,3,4,5或更多个”,或简单的1-3字的短语(否则,我看到单选按钮)。我的问题: 我不是程序员。我当然可以摒弃我自己盲目接受的范式(见下文)(因为它一直在为我的单选按钮列表工作),但我想要一些可重用的东西。对我而言,有时“可重用”意味着类似。
我的范式(或我认为类似的,并会希望我能在复选框或下拉菜单使用什么): 我使用枚举/我的单选按钮dictionarylists如下:
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));
}
}
任何意见/建议? (或者,我能用复选框/下拉菜单继续我的范例吗?)
谢谢!对于我所需要的,这看起来很有希望。 – 2012-03-02 18:38:45