public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (Value == null) { throw new InvalidOperationException($"The '{ValueAttributeName}' attribute must be specified."); } // REVIEW Consider throwing if Checked is null && there is no AspFor var radiosContext = (RadiosContext)context.Items[typeof(RadiosContext)]; var index = radiosContext.Items.Count; var resolvedId = Id ?? (index == 0 ? radiosContext.IdPrefix : $"{radiosContext.IdPrefix}-{index}"); var conditionalId = "conditional-" + resolvedId; var hintId = resolvedId + "-item-hint"; var resolvedChecked = IsChecked ?? (radiosContext.HaveModelExpression ? _modelHelper.GetModelValue( radiosContext.ViewContext, radiosContext.AspFor.ModelExplorer, radiosContext.AspFor.Name) == Value : false); var itemContext = new RadiosItemContext(); TagHelperContent childContent; using (context.SetScopedContextItem(typeof(RadiosItemContext), itemContext)) { childContent = await output.GetChildContentAsync(); } radiosContext.AddItem(new RadiosItem() { Attributes = output.Attributes.ToAttributesDictionary(), IsChecked = resolvedChecked, ConditionalContent = itemContext.Conditional?.content, ConditionalAttributes = itemContext.Conditional?.attributes, ConditionalId = conditionalId, Content = childContent.Snapshot(), IsDisabled = IsDisabled, HintAttributes = itemContext.Hint?.attributes, HintContent = itemContext.Hint?.content, HintId = hintId, Id = resolvedId, InputAttributes = InputAttributes, Value = Value }); if (itemContext.Conditional != null) { radiosContext.SetIsConditional(); } output.SuppressOutput(); }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (Value == null) { throw new InvalidOperationException($"The '{ValueAttributeName}' attribute must be specified."); } var checkboxesContext = (CheckboxesContext)context.Items[typeof(CheckboxesContext)]; var index = checkboxesContext.Items.Count; var resolvedId = Id ?? (index == 0 ? checkboxesContext.IdPrefix : $"{checkboxesContext.IdPrefix}-{index}"); var conditionalId = "conditional-" + resolvedId; var hintId = resolvedId + "-item-hint"; // REVIEW should we throw if AspFor is null and Checked is not specified? var resolvedChecked = IsChecked ?? (checkboxesContext.HaveModelExpression ? _htmlGenerator.GetModelValue( checkboxesContext.ViewContext, checkboxesContext.AspFor.ModelExplorer, checkboxesContext.AspFor.Name) == Value : false); var itemContext = new CheckboxesItemContext(); TagHelperContent childContent; using (context.SetScopedContextItem(typeof(CheckboxesItemContext), itemContext)) { childContent = await output.GetChildContentAsync(); } checkboxesContext.AddItem(new CheckboxesItem() { Attributes = output.Attributes.ToAttributesDictionary(), IsChecked = resolvedChecked, ConditionalContent = itemContext.Conditional?.content, ConditionalAttributes = itemContext.Conditional?.attributes, ConditionalId = conditionalId, Content = childContent.Snapshot(), IsDisabled = IsDisabled, HintAttributes = itemContext.Hint?.attributes, HintContent = itemContext.Hint?.content, HintId = hintId, Id = resolvedId, InputAttributes = InputAttributes, Name = checkboxesContext.ResolvedName, Value = Value }); if (itemContext.Conditional != null) { checkboxesContext.SetIsConditional(); } output.SuppressOutput(); }