private AngularTagBuilder CreateTagBuilder <TProperty>(Expression <Func <TModel, TProperty> > expression, string tagName, IDictionary <string, object> htmlAttributes, ModelMetadata metadata) { var tagBuilder = new AngularTagBuilder(tagName); IEnumerable <ValidationRule> rules = null; if (expression != null) { var expressionText = ExpressionHelper.GetExpressionText(expression); var propertyName = GetPropertyIdentifier(expressionText); var nodeName = GetElementIdentifier(expressionText); tagBuilder.MergeAttribute("id", nodeName, true); tagBuilder.MergeAttribute("name", propertyName, true); tagBuilder.MergeAttribute("ng-model", propertyName, true); if (metadata != null) { rules = GetValidationRules(metadata); } } tagBuilder.MergeAttributes(htmlAttributes, true); if (rules != null) { foreach (var attr in rules.SelectMany(r => r.ValidationParameters)) { tagBuilder.Attributes[attr.Value.Name] = Convert.ToString(attr.Value.Value); } } //tagBuilder.MergeAttributes(extra is IDictionary<string, object> ? (IDictionary<string, object>)extra : HtmlHelper.AnonymousObjectToHtmlAttributes(extra), true); return(tagBuilder); }
public IHtmlString Label <TProperty>(Expression <Func <TModel, TProperty> > expression, IDictionary <string, object> htmlAttributes) { var metadata = ModelMetadata.FromLambdaExpression(expression, _helper.ViewData); string displayName = metadata.DisplayName ?? metadata.PropertyName; var tagBuilder = new AngularTagBuilder("label"); tagBuilder.Attributes["class"] = AngularConfiguration.LabelCssClass; var expressionText = ExpressionHelper.GetExpressionText(expression); var nodeName = GetElementIdentifier(expressionText); tagBuilder.MergeAttribute("for", nodeName); tagBuilder.SetInnerText(displayName); tagBuilder.MergeAttributes(htmlAttributes, true); return(new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal))); }