private static ClientSideViewModel ArrangeFields(Type type, ViewConfiguration config) { var vm = new ClientSideViewModel() { ContainerId = config.ContainerName, FormId = config.FormId, TableName = config.TableName, GetAddress = config.GetAddress, SaveAction = config.SaveAction, FormTab = config.InputTabName }; var properties = type.GetProperties(); foreach (var property in properties) { var csm = new ClientSideModel() { Id = Guid.NewGuid().ToString(), Name = property.Name.Substring(0, 1).ToLower().Replace("ı", "i") + property.Name.Substring(1), FullName = property.Name }; var isRequired = property.GetCustomAttribute <RequiredAttribute>(true); var isHidden = property.GetCustomAttribute <HiddenColumnAttribute>(true); var displayName = property.GetCustomAttribute <DisplayAttribute>(true); var order = property.GetCustomAttribute <OrderableAttribute>(true); var autoComplete = property.GetCustomAttribute <AutoCompleteAttribute>(true); if (property.PropertyType.IsGenericType() && property.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable <>) || property.PropertyType.IsArray) { csm.IsArray = true; } csm.IsNumber = property.PropertyType == typeof(int) || property.PropertyType == typeof(long) || property.PropertyType == typeof(short) || property.PropertyType == typeof(decimal) || property.PropertyType == typeof(double); csm.IsText = property.PropertyType == typeof(string); csm.IsEnum = property.PropertyType.GetTypeInfo().IsEnum; csm.IsBool = property.PropertyType == typeof(bool); csm.IsRequired = isRequired != null; csm.IsHidden = isHidden != null; csm.IsOrderable = order != null; csm.DisplayName = displayName != null ? displayName.Name : property.Name; if (autoComplete != null) { csm.AutoCompleteSource = new ClientSideModel.AutoComplete() { PageSize = autoComplete.PageSize, Url = autoComplete.Url, InputLength = autoComplete.InputLength }; } vm.Models.Add(csm); } return(vm); }
public static HtmlString ViewInitializer <T>(this IHtmlHelper <T> helper, ViewConfiguration config) { var type = helper.GetType().GetGenericArguments().First(); var vm = ArrangeFields(type, config); var settings = new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeHtml }; return(new HtmlString(JsonConvert.SerializeObject(vm, Formatting.None, settings))); }
public static HtmlString DataTable <T>(this IHtmlHelper <T> helper, ViewConfiguration config) { var type = helper.GetType().GetGenericArguments().First(); var vm = ArrangeFields(type, config); var headers = vm.Models.Where(i => !i.IsHidden).Select(i => i.DisplayName).ToList(); var headerColumns = new List <string>(); foreach (var header in headers) { headerColumns.Add(string.Format(HtmlStrings.TableStrings.HEADERCOLUMNCONTENT, header)); } headerColumns.Add(string.Format(HtmlStrings.TableStrings.HEADERCOLUMNCONTENT, "")); var headerRow = string.Format(HtmlStrings.TableStrings.ROWCONTENT, string.Join(Environment.NewLine, headerColumns)); var headerHtml = string.Format(HtmlStrings.TableStrings.TABLEHEAD, headerRow); var table = string.Format(HtmlStrings.TableStrings.TABLE, config.TableName, headerHtml, HtmlStrings.TableStrings.TABLEBODY); return(new HtmlString(table.ToString())); }