public static IHtmlString initEnumeration <T>(this HtmlHelper helper, Enumeration.InputType inputType, string name, object htmlAttributes = null) { IDictionary <string, object> HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); StringBuilder sb = new StringBuilder(); var hidValue = new T[] { }; if (HtmlAttributes["hidevalue"] != null) { hidValue = (T[])HtmlAttributes["hidevalue"]; } var classname = ""; if (HtmlAttributes["class"] != null) { classname = "class=\"" + HtmlAttributes["class"].ToString() + "\""; } var selectValue = new T[] { }; if (HtmlAttributes["selectedvalue"] != null) { selectValue = (T[])HtmlAttributes["selectedvalue"]; } string defaulttext = ""; if (HtmlAttributes["defaulttext"] != null) { defaulttext = HtmlAttributes["defaulttext"].ToString(); } if (inputType == Enumeration.InputType.Select) { int i = 0; string id = name.Replace(".", "_"); sb.Append("<select name=\"" + name + "\" id=\"" + id + "\" " + classname + ">"); if (defaulttext != "") { sb.Append("<option value=\"\" >" + defaulttext + "</option>"); } foreach (T t in Enum.GetValues(typeof(T))) { string strKey = Convert.ToInt32(t).ToString(); Enum en = (Enum)Enum.Parse(t.GetType(), strKey); string strValue = en.GetCnName(); string checkeds = ""; if (selectValue != null) { if (selectValue.Length > 0) { if (selectValue.Contains(t)) { checkeds = "selected"; } } } bool isHide = false; if (hidValue != null) { if (hidValue.Length > 0) { if (hidValue.Contains(t)) { isHide = true; } } } if (!isHide) { sb.Append(" <option value=\"" + strKey + "\" " + checkeds + ">" + strValue + "</option>"); } i++; } sb.Append("/<select>"); } else if (inputType == Enumeration.InputType.CheckBox) { int i = 0; foreach (T t in Enum.GetValues(typeof(T))) { string strKey = Convert.ToInt32(t).ToString(); string strValue = Enum.GetName(typeof(T), t); string id = name + i; string checkeds = ""; if (selectValue != null) { for (int j = 0; j < selectValue.Length; j++) { string key1 = strValue; string key2 = selectValue[j].ToString(); if (key1 == key2) { checkeds = "checked"; break; } } } sb.Append(" <input type=\"checkbox\" name=\"" + name + "\" id=\"" + id + "\" value=\"" + strKey + "\" " + checkeds + " /><label for=\"" + id + "\">" + strValue + "</label>"); i++; } } else if (inputType == Enumeration.InputType.Radio) { int i = 0; foreach (T t in Enum.GetValues(typeof(T))) { string strKey = Convert.ToInt32(t).ToString(); string strValue = Enum.GetName(typeof(T), t); string id = name + i; string checkeds = ""; if (selectValue != null) { if (selectValue.Length > 0) { string key1 = strValue; string key2 = selectValue[0].ToString(); if (key1 == key2) { checkeds = "checked"; } } } sb.Append(" <input type=\"radio\" name=\"" + name + "\" id=\"" + name + "\" value=\"" + strKey + "\" " + checkeds + " /><label for=\"" + id + "\">" + strValue + "</label>"); i++; } } return(new MvcHtmlString(sb.ToString())); }
public static IHtmlString initGoodsType(this HtmlHelper helper, Enumeration.InputType inputType, string name, object htmlAttributes = null) { IDictionary <string, object> HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); StringBuilder sb = new StringBuilder(); var classname = ""; if (HtmlAttributes["class"] != null) { classname = "class=\"" + HtmlAttributes["class"].ToString() + "\""; } var selectValue = ""; if (HtmlAttributes["selectedvalue"] != null) { selectValue = HtmlAttributes["selectedvalue"].ToString(); } string defaulttext = ""; if (HtmlAttributes["defaulttext"] != null) { defaulttext = HtmlAttributes["defaulttext"].ToString(); } if (inputType == Enumeration.InputType.Select) { string id = name.Replace(".", "_"); sb.Append("<select name=\"" + name + "\" id=\"" + id + "\" " + classname + ">"); if (defaulttext != "") { sb.Append("<option value=\"\" >" + defaulttext + "</option>"); } List <KeyValuePair <int, string> > goodsTypes = Lumos.BLL.BizFactory.Product.GetGoodsType(); foreach (var goodsType in goodsTypes) { var checkeds = ""; if (selectValue != null) { if (selectValue == goodsType.Key.ToString()) { checkeds = "selected"; } } string dept = GetDept(goodsType.Key.ToString()); sb.Append(" <option value=\"" + goodsType.Key + "\" " + checkeds + " >" + dept + goodsType.Value + "</option>"); } sb.Append("/<select>"); } return(new MvcHtmlString(sb.ToString())); }