如何将枚举传递给 Html.RadioButtonFor 以获取 MVC 2 RC 2、C# 中的单选按钮列表

发布于 2024-08-20 20:27:49 字数 317 浏览 5 评论 0原文

我尝试使用以下行在 MVC 2 RC 2 (C#) 中渲染单选按钮列表:

<%= Html.RadioButtonFor(model => Enum.GetNames(typeof(DataCarry.ProtocolEnum)),
                        null) %>

但它只是在运行时给出以下异常:

模板只能与字段访问、属性访问、一维数组索引或单参数自定义索引器表达式一起使用。

请问这可能吗?如果可以,怎么做?

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-08-27 20:27:49

您可以在 /Views/Shared/EditorTemplates/Enum.ascx 中创建一个名为“Enum”的模板,

其中包含以下内容:

<%= 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*/)

You can create a template called "Enum" in /Views/Shared/EditorTemplates/Enum.ascx

With the following content:

<%= Html.DropDownList(string.Empty, Enum.GetNames(Model.GetType()).ToList().ConvertAll(e => new SelectListItem() { Text = e.ToString(), Value = e , Selected = e.Equals(Model.ToString())}))  %>

This just enumerates the enum values.

You can call this with

Html.EditorFor(m => m.YourEnumProperty, "Enum" /*The name of the template*/)
随遇而安 2024-08-27 20:27:49

尝试使用 GetValues

Try GetValues instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文