加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何将枚举传递给Html.RadioButtonFor以获取MVC

发布时间:2020-12-16 04:23:57 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试使用以下行在MVC 2 RC 2(C#)中呈现单选按钮列表: %= Html.RadioButtonFor(model = Enum.GetNames(typeof(DataCarry.ProtocolEnum)),null) % 但它只是在运行时给我以下异常: Templates can be used only with field access,property access,singl
我正在尝试使用以下行在MVC 2 RC 2(C#)中呈现单选按钮列表:
<%= Html.RadioButtonFor(model => Enum.GetNames(typeof(DataCarry.ProtocolEnum)),null) %>

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

Templates can be used only with field access,property access,single-dimension array index,or single-parameter custom indexer expressions.

这是可能的吗?如果是的话,请问怎么样?

解决方法

您可以在/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*/)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读