/// <summary> /// Extracts all Keys and Values for the Parameters of the request Url from the given Options via Attributes /// </summary> /// <typeparam name="T">Options Type</typeparam> /// <param name="options">given options</param> /// <param name="url">Url for the request</param> public void BuildUrlOld <T>(T options, ref string url) { IList <Tuple <int, string> > propertyValues = new List <Tuple <int, string> >(); IList <Tuple <string, string> > keyValueProperties = new List <Tuple <string, string> >(); foreach (PropertyInfo property in typeof(T).GetProperties()) { UrlPathAttribute urlPathAttribute = property.GetCustomAttribute <UrlPathAttribute>(); if (urlPathAttribute != null) { int position = urlPathAttribute.Position; string value = (string)property.GetValue(options); if (value.IsNullOrEmpty()) { continue; } propertyValues.Add(new Tuple <int, string>(position, value)); } KeyValueAttribute keyValueAttriute = property.GetCustomAttribute <KeyValueAttribute>(); if (keyValueAttriute != null) { string key = keyValueAttriute.Key; string value = (string)property.GetValue(options); if (key.IsNullOrEmpty() || value.IsNullOrEmpty()) { continue; } keyValueProperties.Add(new Tuple <string, string>(key, value)); } } propertyValues = propertyValues.OrderBy(element => element.Item1).ToList(); foreach (string propertyValue in propertyValues.Select(element => element.Item2)) { url += $"/{propertyValue}"; } url += "?"; foreach (Tuple <string, string> keyValuePair in keyValueProperties) { url += $"{keyValuePair.Item1}={keyValuePair.Item2}&"; } }
// create Invoice public MonetaSdkResult sdkMonetaCreateInvoice(string payer, long payee, decimal amount, string clientTransaction, bool isRegular) { MonetaSdkResult result = new MonetaSdkResult(); try { InvoiceRequest invoiceRequest = new InvoiceRequest(); if (String.Compare(payer, "") != 0) { invoiceRequest.payer = payer; } invoiceRequest.payee = payee; invoiceRequest.amount = amount; invoiceRequest.clientTransaction = clientTransaction; OperationInfo operationInfo = new OperationInfo(); List <KeyValueAttribute> mntAttributes = new List <KeyValueAttribute>(); if (isRegular) { KeyValueAttribute monetaAtribute = new KeyValueAttribute(); monetaAtribute.key = "PAYMENTTOKEN"; monetaAtribute.value = "request"; mntAttributes.Add(monetaAtribute); } operationInfo.attribute = mntAttributes.ToArray(); invoiceRequest.operationInfo = operationInfo; response = client.Invoice(invoiceRequest); result = prepareResult(); } catch (Exception e) { result.error = true; result.errorMessage = e.Message; } return(result); }
private IKeyValueConverter GetConverter(MemberInfo member, Type memberType, KeyValueAttribute keyValueAttr) { IKeyValueConverter converter; if (keyValueAttr?.ConverterType == null) { if (memberType.IsEnum) { return(_enumConverter); } else if (!_keyValueConverters.TryGetValue(memberType, out converter)) { throw new ArgumentException($"KeyValue {member.DeclaringType.FullName}.{member.Name} uses type {memberType.FullName} with no converter"); } } else { if (!typeof(IKeyValueConverter).IsAssignableFrom(keyValueAttr.ConverterType)) { throw new ArgumentException($"{keyValueAttr.ConverterType.FullName} must implement {nameof(IKeyValueConverter)}"); } if (!keyValueAttr.ConverterType.IsPublic || keyValueAttr.ConverterType.IsAbstract) { throw new ArgumentException($"{keyValueAttr.ConverterType.FullName} must be public and instantiable"); } if (!_convertersByConverterType.TryGetValue(keyValueAttr.ConverterType, out converter)) { converter = (IKeyValueConverter)Activator.CreateInstance(keyValueAttr.ConverterType); _convertersByConverterType.Add(keyValueAttr.ConverterType, converter); } } return(converter); }
public static MvcHtmlString CheckBoxListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, DisplayDirection direction, int colOrRows, object htmlAttribute, object itemAttribute) { string resultStr = ""; string checkPropertyName; string displayPropertyName; MemberExpression nameExpr = (MemberExpression)expression.Body; checkPropertyName = nameExpr.Member.Name; PropertyInfo property = typeof(TModel).GetProperty(checkPropertyName); KeyValueAttribute attribute = (KeyValueAttribute)property.GetCustomAttributes(typeof(KeyValueAttribute), false)[0]; if (attribute == null) { throw new Exception(); } displayPropertyName = attribute.DisplayProperty; string validAttribute = ""; //RequiredAttribute requiredAttribute = (RequiredAttribute)property.GetCustomAttributes(typeof(RequiredAttribute), false)[0]; //if (requiredAttribute != null) //{ // validAttribute += " data-val='true' "; // if (String.IsNullOrEmpty(requiredAttribute.ErrorMessage)) // { // string template = "The {0} field is required."; // validAttribute += " data-val-required='" + string.Format(template, displayPropertyName) + "' "; // } // else // { // validAttribute += " data-val-required='" + requiredAttribute.ErrorMessage + "' "; // } //} IEnumerable checkList = (IEnumerable)htmlHelper.ViewData.Eval(displayPropertyName); var isCheckedList = htmlHelper.ViewData.Eval(checkPropertyName) as IEnumerable; IDictionary <string, object> htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttribute); IDictionary <string, object> itemAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(itemAttribute); string htmlAttributeStr = ""; foreach (var item in htmlAttributes) { htmlAttributeStr += " " + item.Key + "='" + item.Value + "' "; } string itemAttributeStr = ""; foreach (var item in itemAttributes) { itemAttributeStr += " " + item.Key + "='" + item.Value + "' "; } string directionAppend = ""; if (direction == DisplayDirection.Vertical) { directionAppend = "<br/>"; } int index = 1; resultStr += "<div " + validAttribute + htmlAttributeStr + ">"; foreach (KeyValueModel check in checkList) { resultStr += "<span " + itemAttributeStr + ">"; string checkedStr = ""; bool isCheck = false; foreach (var checkvalue in isCheckedList) { isCheck = check.Value == checkvalue.ToString(); if (isCheck) { checkedStr = " checked='checked' "; break; } } KeyValueModel checkModel = (KeyValueModel)check; if (check.Disable == "disabled" && check.Disable != null) { resultStr += "<label class='label-multi'><input class='" + check.Disable + "'disabled='" + check.Disable + "'name='" + checkPropertyName + "' type=\"checkbox\" value='" + check.Value + "'" + checkedStr + " ></input>" + " "; resultStr += checkModel.Text + "</label>"; } else { resultStr += "<label class='label-multi'><input name='" + checkPropertyName + "' type=\"checkbox\" value='" + check.Value + "'" + checkedStr + " ></input>" + " "; resultStr += checkModel.Text + "</label>"; } resultStr += "</span>"; if (index % colOrRows == 0) { resultStr += directionAppend; } index = index + 1; } resultStr += "</div>"; MvcHtmlString result = new MvcHtmlString(resultStr); return(result); }
public static MvcHtmlString RadioListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, int cols, object warpHtmlAttribute, object itemHtmlAttribute, int width = 0, int indent = 0) { string tableClassStr = ""; string tdClassStr = ""; string result = ""; string radioPropertyName; string displayPropertyName; MemberExpression nameExpr = (MemberExpression)expression.Body; radioPropertyName = nameExpr.Member.Name; // 获取特性 PropertyInfo property = typeof(TModel).GetProperty(radioPropertyName); KeyValueAttribute attribute = (KeyValueAttribute)property.GetCustomAttributes(typeof(KeyValueAttribute), false)[0]; if (attribute == null) { throw new Exception(); } displayPropertyName = attribute.DisplayProperty; //获取显示列表 和选择值 IEnumerable radioList = (IEnumerable)htmlHelper.ViewData.Eval(displayPropertyName); var checkValue = htmlHelper.ViewData.Eval(radioPropertyName); // 转换objectHtmlAttrbute IDictionary <string, object> wrapKeyValue = HtmlHelper.AnonymousObjectToHtmlAttributes(warpHtmlAttribute); IDictionary <string, object> itemKeyValue = HtmlHelper.AnonymousObjectToHtmlAttributes(itemHtmlAttribute); // 生成Html if (wrapKeyValue.Count > 0) // wrapClassStr { foreach (KeyValuePair <string, object> keyValue in wrapKeyValue) { tableClassStr += keyValue.Key + "=" + keyValue.Value.ToString(); } } //if (itemKeyValue.Count > 0) //{ // foreach (KeyValuePair<string, object> keyValue in itemKeyValue) // { // tdClassStr += keyValue.Key + "='" + keyValue.Value.ToString() + "' "; // } //} int index = 0; result += "<table " + tableClassStr + ">"; foreach (var radio in radioList) { if (index % cols == 0) { result += "<tr>"; } KeyValueModel radioModel = (KeyValueModel)radio; bool isCheck = checkValue == null ? false : checkValue.ToString() == radioModel.Value; if (width > 0) { result += "<td width=\"" + width + "px\" style=\"margin-right:" + indent + "px; display:inline-block;\">"; } else { result += "<td style=\"margin-right:" + indent + "px; display:inline-block;\" " + ">"; //"<td " + tdClassStr + ">"; } result += (htmlHelper.RadioButton(radioPropertyName, radioModel.Value, isCheck, itemHtmlAttribute)).ToHtmlString(); result += radioModel.Text; //ResourceHelper.GetResourceObject(typeof(CommonResource), radioModel.ResourceKey) result += "</td>"; if (index % cols == cols - 1) { result += "</tr>"; } index += 1; } result += "</table>"; return(new MvcHtmlString(result)); }
public static MvcHtmlString CheckListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, bool vaild, DisplayDirection direction, int colOrRows, object htmlAttribute, object itemAttribute, object checkAttribute = null) { string resultStr = ""; string checkPropertyName; string displayPropertyName; MemberExpression nameExpr = (MemberExpression)expression.Body; checkPropertyName = nameExpr.Member.Name; PropertyInfo property = typeof(TModel).GetProperty(checkPropertyName); KeyValueAttribute attribute = (KeyValueAttribute)property.GetCustomAttributes(typeof(KeyValueAttribute), false)[0]; if (attribute == null) { throw new Exception(); } displayPropertyName = attribute.DisplayProperty; string needVaild = ""; if (vaild) { needVaild = "_needVaild"; } string validAttribute = ""; //RequiredAttribute requiredAttribute = (RequiredAttribute)property.GetCustomAttributes(typeof(RequiredAttribute), false)[0]; RequiredAttribute requiredAttribute = new RequiredAttribute(); if (property.GetCustomAttributes(typeof(RequiredAttribute), false).Count() > 0) { requiredAttribute = (RequiredAttribute)property.GetCustomAttributes(typeof(RequiredAttribute), false)[0]; } if (requiredAttribute != null) { validAttribute += " data-val='true' "; if (String.IsNullOrEmpty(requiredAttribute.ErrorMessage)) { string template = "The {0} field is required."; validAttribute += " data-val-required='" + string.Format(template, displayPropertyName) + "' "; } else { validAttribute += " data-val-required='" + ResourceHelper.GetValue(requiredAttribute.ErrorMessage) + "' "; } } IEnumerable checkList = (IEnumerable)htmlHelper.ViewData.Eval(displayPropertyName); var isCheckedList = htmlHelper.ViewData.Eval(checkPropertyName) as IEnumerable; IDictionary <string, object> htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttribute); IDictionary <string, object> itemAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(itemAttribute); IDictionary <string, object> checkAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(checkAttribute); string htmlAttributeStr = ""; foreach (var item in htmlAttributes) { htmlAttributeStr += " " + item.Key + "='" + item.Value + "' "; } string itemAttributeStr = ""; foreach (var item in itemAttributes) { itemAttributeStr += " " + item.Key + "='" + item.Value + "' "; } string checkAttributeStr = ""; foreach (var item in checkAttributes) { checkAttributeStr += " " + item.Key + "='" + item.Value + "' "; } string directionAppend = ""; if (direction == DisplayDirection.Vertical) { directionAppend = "<br/>"; } int index = 1; resultStr += "<div " + htmlAttributeStr + ">"; foreach (KeyValueModel check in checkList) { if (check.Disable == "disabled" && check.Disable != null) { index--; resultStr += "<label style=\"display:none\">"; } else { resultStr += "<label " + itemAttributeStr + ">"; } string checkedStr = ""; bool isCheck = false; if (isCheckedList != null) { foreach (var checkvalue in isCheckedList) { isCheck = check.Value == checkvalue.ToString(); if (isCheck) { checkedStr = " checked='checked' "; break; } } } KeyValueModel checkModel = (KeyValueModel)check; if (check.Disable == "disabled" && check.Disable != null) { resultStr += "<input" + validAttribute + " class='" + check.Disable + "'style=\"display:none\" id='" + checkPropertyName + needVaild + "' name='" + checkPropertyName + "' type=\"checkbox\" value='" + check.Value + "'" + checkedStr + checkAttributeStr + " ></input>" + " "; resultStr += checkModel.Text; } else { resultStr += "<input" + validAttribute + " name='" + checkPropertyName + "' id='" + checkPropertyName + needVaild + "' type=\"checkbox\" value='" + check.Value + "'" + checkedStr + checkAttributeStr + " ></input>" + " "; resultStr += checkModel.Text; } resultStr += "</label>"; if (index % colOrRows == 0) { resultStr += directionAppend; } index = index + 1; } if (resultStr == "<div >") { resultStr += "<label " + itemAttributeStr + " style=\"display:none\"><input" + validAttribute + " name='" + checkPropertyName + "' id='" + checkPropertyName + needVaild + "' type=\"checkbox\" value='1' " + checkAttributeStr + " style=\"visibility:hidden\"></input></label>"; } resultStr += "</div>"; MvcHtmlString result = new MvcHtmlString(resultStr); return(result); }