ASP Tag Helpers 中的@Model.propertyname 是什么?
What is the equivalent of @Model.propertyname in ASP Tag Helpers?
在原版HTML Helpers中,我通常使用<div>@Model.Name</div>
来打印Name
属性,Tag Helpers有类似的功能吗?
正如我在评论中所说,没有生成 div 的标准标签助手(或者我上次看时没有)。这是我们在办公室编写的用于生成 div:
的标签助手示例
[HtmlTargetElement("div", Attributes = FOR_ATTRIBUTE_NAME)]
public class DivTagHelper : TagHelper
{
private const string FOR_ATTRIBUTE_NAME = "si-for";
/// <summary>
/// Creates a new <see cref="DivTagHelper"/>.
/// </summary>
/// <param name="generator">The <see cref="IHtmlGenerator"/>.</param>
public DivTagHelper(IHtmlGenerator generator)
{
Generator = generator;
}
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
protected IHtmlGenerator Generator { get; }
/// <summary>
/// An expression to be evaluated against the current model.
/// </summary>
[HtmlAttributeName(FOR_ATTRIBUTE_NAME)]
public ModelExpression For { get; set; }
/// <inheritdoc />
/// <remarks>Does nothing if <see cref="For"/> is <c>null</c>.</remarks>
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var childContent = output.Content.IsModified ? output.Content.GetContent() :
(await output.GetChildContentAsync()).GetContent();
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (output == null)
{
throw new ArgumentNullException(nameof(output));
}
output.TagName = "div";
string content;
if (For.Metadata.UnderlyingOrModelType == typeof(bool))
{
content = ((bool?) For.Model).ToYesNo();
}
else
{
var displayFormatString = For.ModelExplorer.Metadata.DisplayFormatString;
content = string.IsNullOrEmpty(displayFormatString)
? For.Model?.ToString()
: string.Format(displayFormatString, For.Model);
}
// Honour the model's specified format if there is one.
output.Content.SetContent(content);
output.PostContent.SetHtmlContent(childContent);
}
}
这是一个用法示例:
<li class="TypeFile">
<si-label si-for="FileSize"></si-label>
<div si-for="FileSize" id="FileSize" class="ReadOnlyValue"></div>
<input class="subForm" asp-for="FileSizeInBytes" />
</li>
请注意,"si" 前缀是我们公司的缩写,以确保与现有属性没有歧义。
这是一个输出示例:
<li class="TypeFile">
<label for="FileSize">File Size:</label>
<div id="FileSize" class="ReadOnlyValue">13.700KB</div>
<input class="subForm" type="hidden" id="FileSizeInBytes" name="FileSizeInBytes" value="14029" />
</li>
您可以使用标准构造来完成您的任务:
<input asp-for="Name" class="form-control" />
在原版HTML Helpers中,我通常使用<div>@Model.Name</div>
来打印Name
属性,Tag Helpers有类似的功能吗?
正如我在评论中所说,没有生成 div 的标准标签助手(或者我上次看时没有)。这是我们在办公室编写的用于生成 div:
的标签助手示例[HtmlTargetElement("div", Attributes = FOR_ATTRIBUTE_NAME)]
public class DivTagHelper : TagHelper
{
private const string FOR_ATTRIBUTE_NAME = "si-for";
/// <summary>
/// Creates a new <see cref="DivTagHelper"/>.
/// </summary>
/// <param name="generator">The <see cref="IHtmlGenerator"/>.</param>
public DivTagHelper(IHtmlGenerator generator)
{
Generator = generator;
}
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
protected IHtmlGenerator Generator { get; }
/// <summary>
/// An expression to be evaluated against the current model.
/// </summary>
[HtmlAttributeName(FOR_ATTRIBUTE_NAME)]
public ModelExpression For { get; set; }
/// <inheritdoc />
/// <remarks>Does nothing if <see cref="For"/> is <c>null</c>.</remarks>
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var childContent = output.Content.IsModified ? output.Content.GetContent() :
(await output.GetChildContentAsync()).GetContent();
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (output == null)
{
throw new ArgumentNullException(nameof(output));
}
output.TagName = "div";
string content;
if (For.Metadata.UnderlyingOrModelType == typeof(bool))
{
content = ((bool?) For.Model).ToYesNo();
}
else
{
var displayFormatString = For.ModelExplorer.Metadata.DisplayFormatString;
content = string.IsNullOrEmpty(displayFormatString)
? For.Model?.ToString()
: string.Format(displayFormatString, For.Model);
}
// Honour the model's specified format if there is one.
output.Content.SetContent(content);
output.PostContent.SetHtmlContent(childContent);
}
}
这是一个用法示例:
<li class="TypeFile">
<si-label si-for="FileSize"></si-label>
<div si-for="FileSize" id="FileSize" class="ReadOnlyValue"></div>
<input class="subForm" asp-for="FileSizeInBytes" />
</li>
请注意,"si" 前缀是我们公司的缩写,以确保与现有属性没有歧义。
这是一个输出示例:
<li class="TypeFile">
<label for="FileSize">File Size:</label>
<div id="FileSize" class="ReadOnlyValue">13.700KB</div>
<input class="subForm" type="hidden" id="FileSizeInBytes" name="FileSizeInBytes" value="14029" />
</li>
您可以使用标准构造来完成您的任务:
<input asp-for="Name" class="form-control" />