asp.net-mvc – 如何根据枚举值检查单选按钮
发布时间:2020-12-16 03:41:00 所属栏目:asp.Net 来源:网络整理
导读:我有一个枚举.根据模型带来的价值,我必须检查单选按钮.我怎样才能做到这一点? %=Html.RadioButton("Role",Role.Viewer)%%= .Role.Viewer% %=Html.RadioButton("Role",.Role.Reporter)%%= Role.Reporter% %=Html.RadioButton("Role",Role.User)%%= Role.User
我有一个枚举.根据模型带来的价值,我必须检查单选按钮.我怎样才能做到这一点?
<%=Html.RadioButton("Role",Role.Viewer)%><%= .Role.Viewer%> <%=Html.RadioButton("Role",.Role.Reporter)%><%= Role.Reporter%> <%=Html.RadioButton("Role",Role.User)%><%= Role.User%> 我的枚举将是数字1到3,例如如果选择的枚举值为1,如何检查Role.Viewer? 解决方法
您可以将枚举值强制转换为int而不会出现任何问题:
<%=Html.RadioButton("Role",(int)Role.Viewer)%><%= (int)Role.Viewer%> <%=Html.RadioButton("Role",(int)Role.Reporter)%><%= (int)Role.Reporter%> <%=Html.RadioButton("Role",(int)Role.User)%><%= (int)Role.User%> 请记住,枚举定义隐式具有从0开始的索引.如果您想要更多控制,可以自己手动将int值分配给枚举: public enum Role { Viewer = 1,Reporter = 2,User = 3 } [更新] 根据您的评论,我得到您想要将数据库值绑定到radiolist.您可以使用以下代码执行此操作: <%=Html.RadioButton("Role",(int)Role.Viewer,Model.Role == Role.Viewer)%><%= (int)Role.Viewer%> <%=Html.RadioButton("Role",(int)Role.Reporter,Model.Role == Role.Reporter)%><%= (int)Role.Reporter%> <%=Html.RadioButton("Role",(int)Role.User,Model.Role == Role.User)%><%= (int)Role.User%> 此代码假定数据库值与Model.Role同步,但您可以将其替换为您自己的逻辑.并且有more elegant ways写了radiobutton枚举,但只有3个无线电选项,坚持这个解决方案是安全的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Asp.net MVC scheduler的实现方法详解
- asp.net-core – 在Visual Studio 2015 for ASP.NET Core项
- asp.net-mvc – ViewModels和渲染
- asp.net-mvc – .net mvc仅当视图中有值时才在视图中显示
- asp.net-core – 如何从ASP.NET Core中的Razor View获取Url
- asp.net-core – .net核心和SAML 2.0
- asp.net-mvc-2 – 如何在ASP.NET MVC中实现自定义主体和标识
- asp.net-mvc – Windsor NHibernate ISession MVC
- asp.net – ScriptManager的EnablePartialRendering属性究竟
- asp.net – K运行时使用的.Net完整框架和.Net Core Framewo
推荐文章
站长推荐
- asp.net-core – 如何使用带有IdentityServer4的
- 经典ASP中的会话
- asp.net核心 – 是否可以自主托管没有IIS(不是We
- 推荐最新的Asp.Net Core 下开源的AOP框架 Aspect
- asp.net – 使用UpdatePanel的CollectionPager问
- [asp.net mvc 奇淫巧技] 05 - 扩展ScriptBundle,
- asp.net-mvc-3 – 如何在ASP.NET MVC中创建递归结
- ASP.Net缓存共享
- 为ASP.NET MVC创建一个基于Unity的ControllerFac
- asp.net-mvc – 在.NET MVC 3中使用REST Web服务
热点阅读