|
|
@@ -8,6 +8,7 @@ using Newtonsoft.Json;
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Globalization;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Linq.Expressions;
|
|
|
@@ -170,6 +171,60 @@ namespace GreenTree.Strohrmann.ERP.Web.Extension
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Return HTML markup for the <paramref name="expression"/>, using a display template for a country selection field.
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="TModel">Model type.</typeparam>
|
|
|
+ /// <typeparam name="TValue">Model value type.</typeparam>
|
|
|
+ /// <param name="htmlHelper">HtmlHelper context.</param>
|
|
|
+ /// <param name="expression">Expression for the requested model enumeration value.</param>
|
|
|
+ /// <param name="htmlAttributes">Additional HTML attributes.</param>
|
|
|
+ public static IHtmlContent CountrySelectionFor<TModel, TValue>(
|
|
|
+ this IHtmlHelper<TModel> htmlHelper,
|
|
|
+ Expression<Func<TModel, TValue>> expression,
|
|
|
+ object htmlAttributes = null)
|
|
|
+ {
|
|
|
+ var attributes = htmlAttributes == null
|
|
|
+ ? new string[0]
|
|
|
+ : htmlAttributes.GetType().GetProperties()
|
|
|
+ .Select(p => String.Format("{0}='{1}'", p.Name.Replace("_", "-"), p.GetValue(htmlAttributes)))
|
|
|
+ .ToArray();
|
|
|
+
|
|
|
+ var func = expression.Compile();
|
|
|
+
|
|
|
+ var funcVal = htmlHelper.ViewData.Model == null
|
|
|
+ ? String.Empty
|
|
|
+ : func(htmlHelper.ViewData.Model).ToString();
|
|
|
+
|
|
|
+ var memberInfo = expression.Body.NodeType == ExpressionType.MemberAccess
|
|
|
+ ? ((MemberExpression)expression.Body).Member
|
|
|
+ : null;
|
|
|
+
|
|
|
+ var name = memberInfo == null
|
|
|
+ ? Guid.NewGuid().ToShortString()
|
|
|
+ : memberInfo.Name;
|
|
|
+
|
|
|
+ var selectBuilder = new StringBuilder();
|
|
|
+ selectBuilder.AppendFormat("<select id=\"{0}\" name =\"{0}\">", name);
|
|
|
+
|
|
|
+ var countries = CultureInfo.GetCultures(CultureTypes.AllCultures)
|
|
|
+ .Where(c => c.LCID != CultureInfo.InvariantCulture.LCID && !c.IsNeutralCulture)
|
|
|
+ .Select(c => new RegionInfo(c.LCID))
|
|
|
+ .Distinct();
|
|
|
+
|
|
|
+ foreach (var country in countries)
|
|
|
+ {
|
|
|
+ if (country.EnglishName == funcVal)
|
|
|
+ selectBuilder.AppendFormat("<option selected value=\"{0}\">{1}</option>", country.EnglishName, country.DisplayName);
|
|
|
+ else
|
|
|
+ selectBuilder.AppendFormat("<option value=\"{0}\">{1}</option>", country.EnglishName, country.DisplayName);
|
|
|
+ }
|
|
|
+
|
|
|
+ selectBuilder.Append("</select>");
|
|
|
+
|
|
|
+ return new HtmlString(selectBuilder.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
#endregion
|
|
|
|
|
|
#region Helper
|