如何传递一个枚举Html.RadioButtonFor得到单选按钮列表中MVC 2 RC 2,C# [英] How to pass an enum to Html.RadioButtonFor to get a list of radio buttons in MVC 2 RC 2, C#
本文介绍了如何传递一个枚举Html.RadioButtonFor得到单选按钮列表中MVC 2 RC 2,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图渲染使用下面的行MVC 2 RC 2(C#)一个单选按钮列表:
I'm trying to render a radio button list in MVC 2 RC 2 (C#) using the following line:
<%= Html.RadioButtonFor(model => Enum.GetNames(typeof(DataCarry.ProtocolEnum)),
null) %>
但它只是让我在运行时以下异常:
but it's just giving me the following exception at runtime:
模板只能与现场访问,访问属性,一维数组的索引,或单参数自定义索引前pressions使用。
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
这是可能的,如果是这样,怎么样,好吗?
Is this possible and if so, how, please?
推荐答案
您可以创建一个在/Views/Shared/EditorTemplates/Enum.ascx称为枚举模板
You can create a template called "Enum" in /Views/Shared/EditorTemplates/Enum.ascx
通过以下内容:
<%= Html.DropDownList(string.Empty, Enum.GetNames(Model.GetType()).ToList().ConvertAll(e => new SelectListItem() { Text = e.ToString(), Value = e , Selected = e.Equals(Model.ToString())})) %>
这只是列举枚举值。
您可以调用这个
Html.EditorFor(m => m.YourEnumProperty, "Enum" /*The name of the template*/)
这篇关于如何传递一个枚举Html.RadioButtonFor得到单选按钮列表中MVC 2 RC 2,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文