//把数字转成枚举
public static T[] NumStringsToEnums<T>(string enumNumString ) //where T:Enum
{
if (string.IsNullOrEmpty(enumNumString)) return null;
string[] aryNums = enumNumString.Split(','); Type enumType = typeof(T); List<T> list = new List<T>();
for(int i=0;i< aryNums.Length; i++)
{
int num = 0;
int.TryParse(aryNums[i].Trim(), out num);
if (num == 0) continue; Array enumValues = Enum.GetValues(enumType);
foreach (Enum enumValue in enumValues)
{
Int32 key = Convert.ToInt32(enumValue);
if (key == num)
{
list.Add((T)(object)enumValue);
break;
}
}
}
return list.ToArray();
}
//判断字符串数字是否是枚举中的值
public static bool HasEnumInNumString<T>(string enumNumString, T findValue) //where T:Enum
{
T[] enums = NumStringsToEnums<T>(enumNumString);
if (enums == null) return false; for(int i=0; i<enums.Length; i++)
{
if((int)(object)enums[i]== (int)(object)findValue)
{
return true;
}
}
return false;
} //数字的枚举串变成汉字
public static string[] EnumNumsToStrings<T>(string enumNumString)
{
if (string.IsNullOrEmpty(enumNumString)) return new string[0]; T[] enums = NumStringsToEnums<T>(enumNumString);
string[] chinese = new string[enums.Length];
for(int i=0; i<enums.Length; i++)
{
chinese[i] = GetEnumCustomDescription(enums[i]);
}
return chinese;
} //数字的枚举串变成前端复选框(包括没有选中的)
public static Tuple<T, string, bool> [] EnumNumsToCheckBoxInfo<T>(string enumNumString)
{
Type enumType = typeof(T);
Array enumValues = Enum.GetValues(enumType);
int n = enumValues.Length;
Tuple<T, string, bool>[] tuples = new Tuple<T, string, bool>[n];
for (int i = 0; i < n; i++)
{
T enumValue = (T)(object)enumValues.GetValue(i);
string chinese = GetEnumCustomDescription((Enum)(object)enumValue);//GetDescription((Enum)(object)enumValue,true);
bool found = HasEnumInNumString(enumNumString, enumValue); tuples[i] = new Tuple<T, string, bool>(enumValue,chinese, found);
}
return tuples;
}
//枚举值转换成前端需要的复选框需要的HTML
public static string EnumNumsToCheckBoxHtml<T>(string enumNumString, string inputVarName,string seperator)
{
StringBuilder sb = new StringBuilder();
Tuple<T, string, bool>[] tuples = EnumNumsToCheckBoxInfo<T>(enumNumString);
for (int i = 0; i < tuples.Length; i++)
{
sb.Append("<input type=\"checkbox\" value=\"" + (int)(object)tuples[i].Item1 + "\""
+ (tuples[i].Item3 ? " checked " : "")
+ " name=\"" + inputVarName + "\">" + tuples[i].Item2+ seperator );
//< input type = "checkbox" value = "100" name = "checkboxValue" checked > gif上传阿里云 & nbsp;      
}
return sb.ToString();
}

  调用:

ViewData["bookAttributeHtml"] =
EnumUtil.EnumNumsToCheckBoxHtml<枚举类>("", "前端checkName", "分隔符");

  前端:

 <div class="form-group group-user-group">
<label class="col-sm-2 col-xs-2 control-label">书属性:</label>
<div class="col-sm-10 col-xs-10">
@Html.Raw(ViewData["bookAttributeHtml"])
</div>
</div>

  显示:

C#Enum用Tuple保存值绑定到前端的CheckBox的更多相关文章

  1. WPF实现多值绑定特性以及多值转换

    WPF中的实现 我们首先来看一下常规的绑定 <Window    x:Class="WpfApplicationSample.MainWindow"    xmlns=&qu ...

  2. [vue]v-bind: sytle/class-bind&属性值绑定

    v-bind - style绑定 - class绑定 - 属性值绑定 <!DOCTYPE html> <html lang="en"> <head&g ...

  3. 总结:WPF中MultiBinding多值绑定的方法

    原文:总结:WPF中MultiBinding多值绑定的方法 一.Xaml中绑定代码: <TextBlock  Grid.Row="5" Grid.Column="3 ...

  4. WPF多值绑定及多值转换(MultiBinding和IMultiValueConverter)

    WPF可以使用MultiBinding进行多值绑定,使用IMultiValueConverter进行多值转换 例: (1)转换器 public class ContentConverter : IMu ...

  5. WPF中DatePiker值绑定以及精简查询

    WPF中DatePiker值绑定以及精简查询 1.WPF中DatePiker值绑定 Xaml中值绑定使用Text <DatePicker Text="{Binding strMinDa ...

  6. Django forms 关于select和checkbox设置初始选中值及让前端选中指定值

    Django的forms和models一样很牛逼.他有两种功能,一是生成form表单,还有就是form表单的验证. 这里主要说一下生成form表单时经常用到的需要设置 初始值 / 默认值 的情况. 1 ...

  7. easyui 》 radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中

    获取一组radio被选中项的值var item = $('input[@name=items][@checked]').val();获取select被选中项的文本var item = $(" ...

  8. jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中

    jQuery获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); //为Se ...

  9. SharePoint 2013 新建项目字段自动加载上次保存值

    1.点击进入NewForm.aspx页面,编辑页面,插入Script Editor WebPart,如下图: 2.插入后如下图,拖动AutoRecord WebPart到脚本编辑器上面,防止因为加载顺 ...

随机推荐

  1. 记录linux 命令

    1.du:查询文件或文件夹的磁盘使用空间 如果当前目录下文件和文件夹很多,使用不带参数du的命令,可以循环列出所有文件和文件夹所使用的空间.这对查看究竟是那个地方过大是不利的,所以得指定深入目录的层数 ...

  2. [转] Torch中实现mini-batch RNN

    工作中需要把一个SGD的LSTM改造成mini-batch的LSTM, 两篇比较有用的博文,转载mark https://zhuanlan.zhihu.com/p/34418001 http://ww ...

  3. 【转】Python 面向对象(初级篇)

    [转]Python 面向对象(初级篇) 51CTO同步发布地址:http://3060674.blog.51cto.com/3050674/1689163 概述 面向过程:根据业务逻辑从上到下写垒代码 ...

  4. This Product is covered by one or more of the following......的问题

    DELL台式机安装ubuntu后无法正常启动,黑屏显示:This Product is covered by one or more of the following...... 解决方案:进入BIO ...

  5. UR#13 SRAND

    总感觉这位大仙讲的很清楚:bztminamoto 题意 题目讲的是求 l~r 内所有数的次大质因子,这里设 f(x) 为 x 的次大质因子 我们差分一下就变成求两个前缀和信息了 按照套路,我们考虑 S ...

  6. OpenStack实践系列⑥构建虚拟机实例

    OpenStack实践系列⑥构建虚拟机实例 四.创建一台虚拟机图解网络,并创建一个真实的桥接网络 创建一个单一扁平网络(名字:flat),网络类型为flat,网络适共享的(share),网络提供者:p ...

  7. python学习第39天

    # 数据操作 # 增 # 删 # 改 # 查 # 单表查询 # 多表查询

  8. linux ln 命令使用参数详解(ln -s 软链接)

    ln是linux中一个非常重要的命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...

  9. 12)django-ORM(单表返回数据3种方式)

    单表查询还回数据有3种形式,返回形式不一样,模板调用方式不同 1)返回Queryset里面内容为对象:Business.objects.all() 这里内容显示是对象 2)返回Queryset里面内容 ...

  10. hive学习03-求一年中的最大温度

    知识点: substr.concat函数的使用: row_number() over(distribute by year sort by temp desc)  #按照年分组,按照tmp去排序  需 ...