ソースを参照

Kundenmaske verbessert

Arne Diekmann 5 年 前
コミット
0ddea5a836

+ 55 - 0
GreenTree.Strohrmann.ERP.Web/Extension/HtmlContentExtension.cs

@@ -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

+ 12 - 7
GreenTree.Strohrmann.ERP.Web/Views/Customer/Create.cshtml

@@ -56,13 +56,11 @@
                 </div>
                 <div class="form-group">
                     <label asp-for="Country" class="control-label"></label>
-                    <input asp-for="Country" class="form-control" />
+                    @Html.CountrySelectionFor(m => m.Country, new { @class = "form-control" })
                     <span asp-validation-for="Country" class="text-danger"></span>
                 </div>
             </div>
         </div>
-    </div>
-    <div class="card-deck mt-3">
         <div class="card bg-light" style="max-width: 368px">
             <div class="card-header pb-1">
                 <h6>Sonstige Daten</h6>
@@ -78,12 +76,19 @@
             </div>
         </div>
     </div>
-    <div class="form-group mt-3">
+    <div class="card-deck mt-3">
+
+    </div>
+
+    <hr />
+
+    <div class="form-group d-flex mt-3">
         <input type="submit" value="Erstellen" class="btn btn-primary" />
+        <div class="align-self-center ml-3">
+            <a asp-action="Index">Zurück zur Liste</a>
+        </div>
     </div>
 </form>
 
-<div>
-    <a asp-action="Index">Zurück zur Liste</a>
-</div>
+