【发布时间】: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