// Gets all subProperties that exist private static List <ModelProperty> GetAllSubProperties(ModelItem item) { if (item == null) { return(null); } ModelPropertyCollection subModelProperties = item.Properties; if (subModelProperties == null) { return(null); } List <ModelProperty> subProperties = null; // We want to expose all properties through the model regardless of whether they // are browsable or not. That distinction should be made by the UI utilizing it foreach (ModelProperty subModelProperty in subModelProperties) { if (subProperties == null) { subProperties = new List <ModelProperty>(); } subProperties.Add(subModelProperty); } return(subProperties); }
/// <summary> /// Sets the value of a property with in a model properties collection, if the property doesn't exist no action is taken. /// </summary> /// <param name="properties">The model property collection.</param> /// <param name="propertyName">Name of the property to set.</param> /// <param name="value">The value to set.</param> public static void SetValue(this ModelPropertyCollection properties, string propertyName, object value) { ModelProperty mp = properties.Find(propertyName); if (mp != null) { mp.SetValue(value); } }
private async Task BindModelCoreAsync(ModelBindingContext bindingContext) { ModelPropertyCollection propertiesRuntime = new ModelPropertyCollection(new List <ModelMetadata>()); //Type Definition if (Int32.TryParse(bindingContext.FieldName, out int index)) { var stackField = typeof(DefaultModelBindingContext).GetField("_stack", BindingFlags.Instance | BindingFlags.NonPublic); dynamic stack = stackField.GetValue(bindingContext); dynamic collectionModel = null; foreach (var stackItem in stack) { var prop = stackItem.GetType().GetField("_Model", BindingFlags.Instance | BindingFlags.NonPublic); FieldInfo[] fields = stackItem.GetType().DeclaredFields; var field = fields.Where(f => f.Name == "Model").First(); collectionModel = field.GetValue(stackItem); } if (index < collectionModel.Length) { var model = collectionModel[index]; bindingContext.Model = model; propertiesRuntime = ((DynamicFormsModelMetadata)bindingContext.ModelMetadata).Contextualize(model); } } else { propertiesRuntime = ((DynamicFormsModelMetadata)_modelBinderProviderContext.Metadata).Contextualize(bindingContext.Model); } //New Instance if required if (bindingContext.Model == null) { bindingContext.Model = await CreateModelAsync(bindingContext); propertiesRuntime = ((DynamicFormsModelMetadata)bindingContext.ModelMetadata).Contextualize(bindingContext.Model); } var propertyBinders = new Dictionary <ModelMetadata, IModelBinder>(); //https://github.com/aspnet/AspNetCore/blob/c565386a3ed135560bc2e9017aa54a950b4e35dd/src/Mvc/Mvc.Core/src/ModelBinding/Binders/ComplexTypeModelBinderProvider.cs for (var i = 0; i < propertiesRuntime.Count; i++) { var property = propertiesRuntime[i]; ((DynamicFormsModelMetadata)property).NormalHashing = true; propertyBinders.Add(property, _modelBinderProviderContext.CreateBinder(property)); ((DynamicFormsModelMetadata)property).NormalHashing = false; } ((DynamicFormsModelMetadata)_modelBinderProviderContext.Metadata).NormalHashing = false; var complexModelBinder = new CustomComplexTypeModelBinder(propertyBinders); await complexModelBinder.BindModelAsync(bindingContext); }
public Enumerator( ModelPropertyCollection properties, string key, object model) { _properties = properties; _key = key; _model = model; _index = -1; }
internal ActionExecutionModelMetadata( IModelMetadataProvider modelMetadataProvider, ICompositeMetadataDetailsProvider detailsProvider, ActionExecutionModel model) : base(modelMetadataProvider, detailsProvider, CreateMetadataDetails(model)) { this.model = model; this.modelMetadataProvider = modelMetadataProvider; this.detailsProvider = detailsProvider; this.properties = new ModelPropertyCollection(CreatePropertiesMetadata()); }
// we have two versions of this. This one is from the metadata only public static string GetKeyProperty(ModelPropertyCollection properties) { string keyProperty = null; foreach (var p in properties) { // test for key var keyAttribute = Utility.GetAttribute <KeyAttribute>(p); if (keyAttribute != null) { keyProperty = p.Name; } } // if we do not have a key search for a property called Id if (string.IsNullOrWhiteSpace(keyProperty)) { keyProperty = properties.Where(p => p.Name.ToLower() == "id").FirstOrDefault()?.Name; } return(keyProperty); }
public override void Process(TagHelperContext context, TagHelperOutput output) { output.TagName = null; if (For == null) { return; } TagBuilder headerRow = new TagBuilder("tr"); TagBuilder tbody = new TagBuilder("tbody"); if (IsCollection) { ModelPropertyCollection propertyMetadata = For.Metadata.ElementMetadata.Properties; foreach (string propertyName in Properties ?? propertyMetadata.Select(property => property.Name)) { string sentenceCasePropertyName = ToSentenceCase(propertyName); TagBuilder header = new TagBuilder("th"); header.Attributes["scope"] = "col"; header.InnerHtml.SetContent(sentenceCasePropertyName); headerRow.InnerHtml.AppendHtml(header); } foreach (object item in (IEnumerable)For.Model) { TagBuilder row = new TagBuilder("tr"); foreach (ModelMetadata property in propertyMetadata) { object propertyValue = property.PropertyGetter(item); string propertyString; switch (propertyValue) { case string value: propertyString = value; break; case IEnumerable <object> enumerable: propertyString = string.Join(", ", enumerable); break; default: propertyString = propertyValue?.ToString(); break; } row.InnerHtml.AppendHtml($"<td>{propertyString}</td>"); } tbody.InnerHtml.AppendHtml(row); } } else { foreach (ModelMetadata property in For.Metadata.Properties) { string sentenceCasePropertyName = ToSentenceCase(property.Name); TagBuilder header = new TagBuilder("th"); header.Attributes["scope"] = "row"; header.InnerHtml.SetContent(sentenceCasePropertyName); TagBuilder row = new TagBuilder("tr"); row.InnerHtml.AppendHtml(header); row.InnerHtml.AppendHtml($"<td>{property.PropertyGetter(For.Model)}</td>"); tbody.InnerHtml.AppendHtml(row); } foreach (string header in new[] { "Property", "Value" }) { TagBuilder th = new TagBuilder("th"); th.Attributes["scope"] = "col"; th.InnerHtml.SetContent(header); headerRow.InnerHtml.AppendHtml(th); } } TagBuilder thead = new TagBuilder("thead"); thead.AddCssClass("thead-dark"); thead.InnerHtml.AppendHtml(headerRow); TagBuilder table = new TagBuilder("table"); table.AddCssClass("table"); table.InnerHtml.AppendHtml(thead); table.InnerHtml.AppendHtml(tbody); output.PostContent.SetHtmlContent(table); }