Razor MVC查看两部分问题 [英] Razor MVC view two part question

查看:65
本文介绍了Razor MVC查看两部分问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题1:我的模型具有活动属性。 'Active'属性的值是'Y'或'N'的字符。我将使用复选框助手的单选按钮帮助程序。实现这个的最佳方法是什么?



问题2 :(更难):现在,因为我们使用的是Oracle数据库模型的用户访问属性,在C#中是一个枚举,是十进制(0.0,1.0,2.0)而不是整数。 Enum看起来像这样:



enum APPRole {SimpleUser,AdvancedUser,ExpertUser};



现在,如何使用帮助器下拉列表使用我的模型的用户访问十进制数据属性来比较枚举,然后显示相应角色的字符串?



非常感谢!









Steve Holdorf

Question 1: I have a Model that has an 'Active' property. The 'Active' property's value is a char either 'Y' or "N". I am going to use either a radio button helper of a checkbox helper. What is the best way to implement this?

Question 2: (more difficult): Now, since we are using an Oracle database the Models 'User Access' property, which in C# is an Enum, is a decimal (0.0 ,1.0, 2.0) and not an integer. The Enum looks like this:

enum APPRole {SimpleUser, AdvancedUser, ExpertUser};

Now, how do I use a helper dropdown to use my Model's 'User Access' decimal data property to compare to the enum and then display strings of the appropriate role?

Big Thanks!




Steve Holdorf

推荐答案

  1. 谁告诉你这个数据类型应该是一个字符?



    如果你认为UI应该显示'Y'或'N',这并不意味着这应该是你的类型。



    类型 char 有太多的值,但您需要只有两个值的类型。最好的类型是 bool ;另一种选择是具有两个值的枚举类型。但 bool 仍然更好。你总是可以将它映射到一组两个字符串(为什么不是是和否?)或你需要的任何其他UI。



    一般来说,将UI与项目的其他方面完全隔离开来。粗略地说,这就是MVC的设计目标。
  2. 适当的枚举类型是:

  1. Who told you this data type should be a character?

    If you think that the UI should show 'Y' or 'N', it does not mean that this should be your type.

    The type char has too many values, but you need the type which has only two values. The best type for that is bool; another option would be enumeration type with two values. But bool is still better. You can always map it onto a set of two strings (why not "Yes" and "No"?) or anything else you would need to your UI.

    Generally, thoroughly isolate UI from other aspects of your project. Roughly speaking, this what is MVC is designed for.
  2. The adequate enumeration type would be:
enum APPRole : byte {
   SimpleUser    = 0,
   AdvancedUser  = 1,
   ExpertUser    = 2,
};



巧合的是,即使您省略了这些整数值,枚举成员的基础整数值的默认规则也会给出相同的值。



现在,没有什么能阻止你处理十进制值(我不知道为什么你需要它们,但我们假设你是对的,即使我怀疑它;如果你解释为什么你需要那些0.0,1.0,和2.0值,我们可以讨论它。)你拥有一切。像这样的陈述将为您提供正确的分配和正确的比较:


By coincidence, even if you omitted those integer values, the default rules for underlying integer values for enumeration members would give you the same values.

Now, nothing prevents you from working decimal values (I have no idea why would you need them, but let's assume you are right, even though I doubt it; if you explain why do you need those 0.0, 1.0, and 2.0 values, we can discuss it.) You have everything. The statements like this one will give you correct assignment and correct comparison:

decimal simpleUser = (byte)APPRole.SimpleUser;

//...

decimal value = //...
if (value == (byte)APPRole.SimpleUser) { /* ... */ }





依此类推......





-SA


这篇关于Razor MVC查看两部分问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆