嵌入在另一个标签助手代码中的标签助手不呈现
Tag Helper Embedded in Another Tag Helper's Code Doesn't Render
我正在尝试使用 .net Core 2.0 中的以下自定义标签助手来简化长表单的创建:
@model ObApp.Web.Models.ViewComponents.FormFieldViewModel
<span>Debug - Paramater value: @Model.FieldFor</span>
<div class="form-group">
<label asp-for="@Model.FieldFor"></label>
<input asp-for="@Model.FieldFor" class="form-control" />
</div>
这看起来很简单,但我在使用时得到了(对我来说)意想不到的结果:
<vc:form-field field-for="PersonEmail"></vc:form-field>
预期结果
<span>Debug - Paramater value: PersonEmail</span>
<div class="form-group">
<label for="PersonEmail">Email</label>
<input name="PersonEmail" class="form-control" id="PersonEmail"
type="text" value="PersonEmail">
</div>
实际结果
<span>Debug - Paramater value: PersonEmail</span>
<div class="form-group">
<label for="FieldFor">FieldFor</label>
<input name="FieldFor" class="form-control" id="FieldFor"
type="text" value="PersonEmail">
</div>
我已经尝试删除 @Model.FieldFor 周围的引号,以及其他一些语法更改。
有什么建议吗?
谢谢!
在 .net 核心中,您不需要在 asp-for="@Model.FieldFor"
中使用 @,如下例
@model ObApp.Web.Models.ViewComponents.FormFieldViewModel
<span>Debug - Paramater value: @Model.FieldFor</span>
<div class="form-group">
<label asp-for="model.FieldFor"></label>
<input asp-for="model.FieldFor" class="form-control" />
</div>
正如其他人向我指出的那样,可能无法按照我在发布此问题时最初希望的方式直接嵌入标签助手。因此,我将代码重构为以编程方式 "new up" 所需的标签助手。
我的最终解决方案比我预期的要多得多,但从长远来看 运行 它将节省大量时间来开发我计划的表单密集型应用程序。
Objective
我的目标是通过使用这个自定义标签助手来加速表单的创建,例如:
<formfield asp-for="OrganizationName"></formfield>
要生成这些内置的 Razor 标签助手:
<div class="form-group">
<div class="row">
<label class="col-md-3 col-form-label" for="OrganizationName">Company Name</label>
<div class="col-md-9">
<input name="OrganizationName" class="form-control" id="OrganizationName" type="text" value="" data-val-required="The Company Name field is required." data-val="true" data-val-maxlength-max="50" data-val-maxlength="Maximum company name length is 50 characters.">
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="OrganizationName"></span>
</div>
</div>
</div>
我最初的工作解决方案
这是简单案例的首关测试方案。 IE。对于默认的硬编码 classes 和文本框输入类型。
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ObApp.Web.TagHelpers
{
// Builds form elements to generate the following (for example):
// <div class="form-group">
// <div class="row">
// <input ... >Email</input>
// <div>
// <input type="text" ... />
// <span class="field-validation-valid ... ></span>
// </div>
// </div>
// </div>
public class FormfieldTagHelper : TagHelper
{
private const string _forAttributeName = "asp-for";
private const string _defaultWraperDivClass = "form-group";
private const string _defaultRowDivClass = "row";
private const string _defaultLabelClass = "col-md-3 col-form-label";
private const string _defaultInputClass = "form-control";
private const string _defaultInnerDivClass = "col-md-9";
private const string _defaultValidationMessageClass = "";
public FormfieldTagHelper(IHtmlGenerator generator)
{
Generator = generator;
}
[HtmlAttributeName(_forAttributeName)]
public ModelExpression For { get; set; }
public IHtmlGenerator Generator { get; }
[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
// Replace this parent tag helper with div tags wrapping the entire form block
output.TagName = "div";
output.Attributes.SetAttribute("class", _defaultWraperDivClass);
// Manually new-up each child asp form tag helper element
TagHelperOutput labelElement = await CreateLabelElement(context);
TagHelperOutput inputElement = await CreateInputElement(context);
TagHelperOutput validationMessageElement = await CreateValidationMessageElement(context);
// Wrap input and validation with column div
IHtmlContent innerDiv = WrapElementsWithDiv(
new List<IHtmlContent>()
{
inputElement,
validationMessageElement
},
_defaultInnerDivClass
);
// Wrap all elements with a row div
IHtmlContent rowDiv = WrapElementsWithDiv(
new List<IHtmlContent>()
{
labelElement,
innerDiv
},
_defaultRowDivClass
);
// Put everything into the innerHtml of this tag helper
output.Content.SetHtmlContent(rowDiv);
}
private async Task<TagHelperOutput> CreateLabelElement(TagHelperContext context)
{
LabelTagHelper labelTagHelper =
new LabelTagHelper(Generator)
{
For = this.For,
ViewContext = this.ViewContext
};
TagHelperOutput labelOutput = CreateTagHelperOutput("label");
await labelTagHelper.ProcessAsync(context, labelOutput);
labelOutput.Attributes.Add(
new TagHelperAttribute("class", _defaultLabelClass));
return labelOutput;
}
private async Task<TagHelperOutput> CreateInputElement(TagHelperContext context)
{
InputTagHelper inputTagHelper =
new InputTagHelper(Generator)
{
For = this.For,
ViewContext = this.ViewContext
};
TagHelperOutput inputOutput = CreateTagHelperOutput("input");
await inputTagHelper.ProcessAsync(context, inputOutput);
inputOutput.Attributes.Add(
new TagHelperAttribute("class", _defaultInputClass));
return inputOutput;
}
private async Task<TagHelperOutput> CreateValidationMessageElement(TagHelperContext context)
{
ValidationMessageTagHelper validationMessageTagHelper =
new ValidationMessageTagHelper(Generator)
{
For = this.For,
ViewContext = this.ViewContext
};
TagHelperOutput validationMessageOutput = CreateTagHelperOutput("span");
await validationMessageTagHelper.ProcessAsync(context, validationMessageOutput);
return validationMessageOutput;
}
private IHtmlContent WrapElementsWithDiv(List<IHtmlContent> elements, string classValue)
{
TagBuilder div = new TagBuilder("div");
div.AddCssClass(classValue);
foreach(IHtmlContent element in elements)
{
div.InnerHtml.AppendHtml(element);
}
return div;
}
private TagHelperOutput CreateTagHelperOutput(string tagName)
{
return new TagHelperOutput(
tagName: tagName,
attributes: new TagHelperAttributeList(),
getChildContentAsync: (s, t) =>
{
return Task.Factory.StartNew<TagHelperContent>(
() => new DefaultTagHelperContent());
}
);
}
}
}
下一步 Steps/Suggested 改进
这适用于没有验证错误的文本框。调整默认 CSS 后,我计划采取的下一步是:
- 在出现验证错误时显示正确的 CSS class 格式属性。此时标签助手对我来说将是"working"。
- 将硬编码的 CSS class 移至站点配置文件。
- 绑定到视图中的 HTML 属性以允许传入非默认 classes。另一种方法是传入非默认形式 class通过 ViewModel。
- 检测非文本框输入类型并相应地设置格式。
感谢@Chris Pratt 让我在这方面朝着正确的方向开始。
我正在尝试使用 .net Core 2.0 中的以下自定义标签助手来简化长表单的创建:
@model ObApp.Web.Models.ViewComponents.FormFieldViewModel
<span>Debug - Paramater value: @Model.FieldFor</span>
<div class="form-group">
<label asp-for="@Model.FieldFor"></label>
<input asp-for="@Model.FieldFor" class="form-control" />
</div>
这看起来很简单,但我在使用时得到了(对我来说)意想不到的结果:
<vc:form-field field-for="PersonEmail"></vc:form-field>
预期结果
<span>Debug - Paramater value: PersonEmail</span>
<div class="form-group">
<label for="PersonEmail">Email</label>
<input name="PersonEmail" class="form-control" id="PersonEmail"
type="text" value="PersonEmail">
</div>
实际结果
<span>Debug - Paramater value: PersonEmail</span>
<div class="form-group">
<label for="FieldFor">FieldFor</label>
<input name="FieldFor" class="form-control" id="FieldFor"
type="text" value="PersonEmail">
</div>
我已经尝试删除 @Model.FieldFor 周围的引号,以及其他一些语法更改。
有什么建议吗?
谢谢!
在 .net 核心中,您不需要在 asp-for="@Model.FieldFor"
中使用 @,如下例
@model ObApp.Web.Models.ViewComponents.FormFieldViewModel
<span>Debug - Paramater value: @Model.FieldFor</span>
<div class="form-group">
<label asp-for="model.FieldFor"></label>
<input asp-for="model.FieldFor" class="form-control" />
</div>
正如其他人向我指出的那样,可能无法按照我在发布此问题时最初希望的方式直接嵌入标签助手。因此,我将代码重构为以编程方式 "new up" 所需的标签助手。
我的最终解决方案比我预期的要多得多,但从长远来看 运行 它将节省大量时间来开发我计划的表单密集型应用程序。
Objective
我的目标是通过使用这个自定义标签助手来加速表单的创建,例如:
<formfield asp-for="OrganizationName"></formfield>
要生成这些内置的 Razor 标签助手:
<div class="form-group">
<div class="row">
<label class="col-md-3 col-form-label" for="OrganizationName">Company Name</label>
<div class="col-md-9">
<input name="OrganizationName" class="form-control" id="OrganizationName" type="text" value="" data-val-required="The Company Name field is required." data-val="true" data-val-maxlength-max="50" data-val-maxlength="Maximum company name length is 50 characters.">
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="OrganizationName"></span>
</div>
</div>
</div>
我最初的工作解决方案
这是简单案例的首关测试方案。 IE。对于默认的硬编码 classes 和文本框输入类型。
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ObApp.Web.TagHelpers
{
// Builds form elements to generate the following (for example):
// <div class="form-group">
// <div class="row">
// <input ... >Email</input>
// <div>
// <input type="text" ... />
// <span class="field-validation-valid ... ></span>
// </div>
// </div>
// </div>
public class FormfieldTagHelper : TagHelper
{
private const string _forAttributeName = "asp-for";
private const string _defaultWraperDivClass = "form-group";
private const string _defaultRowDivClass = "row";
private const string _defaultLabelClass = "col-md-3 col-form-label";
private const string _defaultInputClass = "form-control";
private const string _defaultInnerDivClass = "col-md-9";
private const string _defaultValidationMessageClass = "";
public FormfieldTagHelper(IHtmlGenerator generator)
{
Generator = generator;
}
[HtmlAttributeName(_forAttributeName)]
public ModelExpression For { get; set; }
public IHtmlGenerator Generator { get; }
[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
// Replace this parent tag helper with div tags wrapping the entire form block
output.TagName = "div";
output.Attributes.SetAttribute("class", _defaultWraperDivClass);
// Manually new-up each child asp form tag helper element
TagHelperOutput labelElement = await CreateLabelElement(context);
TagHelperOutput inputElement = await CreateInputElement(context);
TagHelperOutput validationMessageElement = await CreateValidationMessageElement(context);
// Wrap input and validation with column div
IHtmlContent innerDiv = WrapElementsWithDiv(
new List<IHtmlContent>()
{
inputElement,
validationMessageElement
},
_defaultInnerDivClass
);
// Wrap all elements with a row div
IHtmlContent rowDiv = WrapElementsWithDiv(
new List<IHtmlContent>()
{
labelElement,
innerDiv
},
_defaultRowDivClass
);
// Put everything into the innerHtml of this tag helper
output.Content.SetHtmlContent(rowDiv);
}
private async Task<TagHelperOutput> CreateLabelElement(TagHelperContext context)
{
LabelTagHelper labelTagHelper =
new LabelTagHelper(Generator)
{
For = this.For,
ViewContext = this.ViewContext
};
TagHelperOutput labelOutput = CreateTagHelperOutput("label");
await labelTagHelper.ProcessAsync(context, labelOutput);
labelOutput.Attributes.Add(
new TagHelperAttribute("class", _defaultLabelClass));
return labelOutput;
}
private async Task<TagHelperOutput> CreateInputElement(TagHelperContext context)
{
InputTagHelper inputTagHelper =
new InputTagHelper(Generator)
{
For = this.For,
ViewContext = this.ViewContext
};
TagHelperOutput inputOutput = CreateTagHelperOutput("input");
await inputTagHelper.ProcessAsync(context, inputOutput);
inputOutput.Attributes.Add(
new TagHelperAttribute("class", _defaultInputClass));
return inputOutput;
}
private async Task<TagHelperOutput> CreateValidationMessageElement(TagHelperContext context)
{
ValidationMessageTagHelper validationMessageTagHelper =
new ValidationMessageTagHelper(Generator)
{
For = this.For,
ViewContext = this.ViewContext
};
TagHelperOutput validationMessageOutput = CreateTagHelperOutput("span");
await validationMessageTagHelper.ProcessAsync(context, validationMessageOutput);
return validationMessageOutput;
}
private IHtmlContent WrapElementsWithDiv(List<IHtmlContent> elements, string classValue)
{
TagBuilder div = new TagBuilder("div");
div.AddCssClass(classValue);
foreach(IHtmlContent element in elements)
{
div.InnerHtml.AppendHtml(element);
}
return div;
}
private TagHelperOutput CreateTagHelperOutput(string tagName)
{
return new TagHelperOutput(
tagName: tagName,
attributes: new TagHelperAttributeList(),
getChildContentAsync: (s, t) =>
{
return Task.Factory.StartNew<TagHelperContent>(
() => new DefaultTagHelperContent());
}
);
}
}
}
下一步 Steps/Suggested 改进
这适用于没有验证错误的文本框。调整默认 CSS 后,我计划采取的下一步是:
- 在出现验证错误时显示正确的 CSS class 格式属性。此时标签助手对我来说将是"working"。
- 将硬编码的 CSS class 移至站点配置文件。
- 绑定到视图中的 HTML 属性以允许传入非默认 classes。另一种方法是传入非默认形式 class通过 ViewModel。
- 检测非文本框输入类型并相应地设置格式。
感谢@Chris Pratt 让我在这方面朝着正确的方向开始。