public JsonResult GetEnumNameValue(EnumFromType type) { Dictionary <Int32, String> dictionary = new Dictionary <Int32, String>(); switch (type) { case EnumFromType.组织机构性质: dictionary = EnumUtil.EnumToDictionary(typeof(EnumOrgNature), e => e.ToString()); break; case EnumFromType.组织机构状态: dictionary = EnumUtil.EnumToDictionary(typeof(EnumOrgState), e => e.ToString()); break; case EnumFromType.人员状态: dictionary = EnumUtil.EnumToDictionary(typeof(EnumUserNature), e => e.ToString()); break; case EnumFromType.角色状态: dictionary = EnumUtil.EnumToDictionary(typeof(EnumRoleState), e => e.ToString()); break; case EnumFromType.省市区县: dictionary = EnumUtil.EnumToDictionary(typeof(EnumDistrictLevelType), e => e.ToString()); break; } IList <NameValue <int> > nameValue = new List <NameValue <int> >(); foreach (var dic in dictionary) { nameValue.Add(new NameValue <int>(dic.Value, dic.Key)); } return(Json(nameValue)); }
/// <summary> /// 枚举控件下拉框 /// </summary> /// <param name="htmlHelper"></param> /// <param name="input">实体</param> /// <returns></returns> public static MvcHtmlString DropDownListEnum(this HtmlHelper htmlHelper, DropDownListEnumInput input) { var stringBuilder = new StringBuilder("<select id='" + input.Id + "' name='" + input.Name + "'" + input.HtmlAttributes + ">"); if (input.NeedDefault) { stringBuilder.Append("<option value=''>=请选择=</option>"); } var dictionarys = EnumUtil.EnumToDictionary((Type)input.EnumType, e => e.ToString()); foreach (var dic in dictionarys) { if (input.SelectedVal != null) { switch (input.CompareType) { case EnumCompareType.Text: if (dic.Value == input.SelectedVal) { stringBuilder.Append(" <option value='" + dic.Key + "' selected='selected'>" + dic.Value + "</option> "); } else { stringBuilder.Append(" <option value='" + dic.Key + "' >" + dic.Value + "</option> "); } break; case EnumCompareType.Value: if (dic.Key == input.SelectedVal) { stringBuilder.Append(" <option value='" + dic.Key + "' selected='selected'>" + dic.Value + "</option> "); } else { stringBuilder.Append(" <option value='" + dic.Key + "' >" + dic.Value + "</option> "); } break; default: stringBuilder.Append(" <option value='" + dic.Key + "' >" + dic.Value + "</option> "); break; } } else { stringBuilder.Append(" <option value='" + dic.Key + "' >" + dic.Value + "</option> "); } } stringBuilder.Append("</select>"); return(new MvcHtmlString(stringBuilder.ToString())); }
public ActionResult Register() { Dictionary <int, string> Sex = EnumUtil.EnumToDictionary(enumState.userSex.Else.GetType()); List <SelectListItem> Sexlst = new List <SelectListItem>(); foreach (var item in Sex) { Sexlst.Add(new SelectListItem() { Text = item.Value, Value = item.Key.ToString() }); } int i = 0; int d = 2 / i; IEnumerable <SelectListItem> ISexlst = Sexlst; ViewData["Sex"] = ISexlst; return(View()); }