问题描述
我最近使用Razor Pages开始了我的第一个项目,我使用的是EF Core,并将我的所有模型都放入CRUD Razor Pages中.我注意到的是,生成的创建和编辑剃刀页面"使用标签助手"来显示数据.即
I recently started my first project using Razor Pages, I am using EF Core and have scaffolded all of my models into CRUD Razor Pages. Something I noticed was that the Create and Edit Razor Pages that are generated use Tag Helpers to display the data. i.e.
<div class="form-group">
<label asp-for="ViewModel.Name" class="control-label"></label>
<input asp-for="ViewModel.Name" class="form-control" />
<span asp-validation-for="ViewModel.Name" class="text-danger"></span>
</div>
索引,详细信息和删除页面使用Html帮助器显示数据.即
Whereas the Index, Details, and Delete Pages use Html Helpers to display the data. i.e.
<dt>
@Html.DisplayNameFor(model => model.ViewModel.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.ViewModel.Name)
</dd>
此 Microsoft的链接说:标记助手减少了Razor视图中HTML和C#之间的显式转换.在许多情况下,HTML助手为特定的标记助手提供了另一种方法,但重要的是要认识到标记助手不会替换HTML Helper,并且每个HTML Helper都没有一个Tag Helper.".
This link from Microsoft says that "Tag Helpers reduce the explicit transitions between HTML and C# in Razor views. In many cases, HTML Helpers provide an alternative approach to a specific Tag Helper, but it's important to recognize that Tag Helpers don't replace HTML Helpers and there's not a Tag Helper for each HTML Helper.".
但是这些基本显示字段当然可以与Tag Helpers一起显示.在我看来,这似乎是一个相当琐碎的不一致之处,我认为最好是仅在绝对需要时才使用Tag Helpers和Html Helpers,这是更好的做法.
But certainly these basic display fields could be displayed with Tag Helpers. This seems to me like a rather frivolous inconsistency and I would think that it would be better practice to use Tag Helpers primarily and Html Helpers only when absolutely neccecary.
我认为开发人员有这样做的理由,但是我可能没有想到.有人可以帮我照一下吗?
I assume that the developers had a reason for doing it this way, but what that might be escapes me. Could anyone please shine some light on this for me please?
推荐答案
简单:没有标签助手可以显示.
Simple: there's no tag helpers for display.
展开:
为输入之类的东西配备标签助手是很有意义的:与之关联的是特定的HTML标签.显示会使事情变得更加模糊.每个开发人员都可能希望使用不同的标签来显示:span
,strong
,th
,dt
等.这里没有一种适合的尺寸.您甚至可能根本不需要标签.默认情况下,Html.DisplayFor
和Html.DisplayNameFor
仅输出值,不带任何标签.
Having tag helpers for things like inputs makes sense: there's specific HTML tags associated with that. Things get a lot more blurry with display. Each individual developer might want to use different tags for display: span
, strong
, th
, dt
, etc. There's no one-size fits all here. You might not even want a tag at all. By default Html.DisplayFor
and Html.DisplayNameFor
just output the values, without any tags.
此外,这些都是模板化的帮助程序,这意味着您作为开发人员可以实际将模板添加到Views\Shared\DisplayTemplates
并自定义事物的显示方式.例如,您可以在String.cshtml
视图中输入类似以下内容:
Also, these are templated helpers, which means you as a developer can actually add templates to Views\Shared\DisplayTemplates
and customize how things are displayed. For example, you could at a String.cshtml
view there, with something like:
@model string
<strong>@Model</strong>
现在,使用Html.DisplayFor
显示的任何字符串属性都将包装在strong
标记中.标签助手无法实现这种级别的自定义.
And now, any string property displayed using Html.DisplayFor
would be wrapped in strong
tags. This level of customization wouldn't be possible with tag helpers.
编辑
FWIW,如果您真的想使用标签助手,则可以添加自己的标签助手.例如:
FWIW, if you really want to use tag helpers, you can just add your own. For example:
public class DisplayNameTagHelper : TagHelper
{
public ModelExpression For { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "strong";
output.Content.SetContent(For.Metadata.DisplayName);
}
}
然后在您看来:
<display-name for="MyProperty" />
写DisplayTagHelper
在概念上类似,但难度更高.您可以从For.Model
获取值.但是,将其键入为object
.使用For.Metadata
上可用的信息,您需要确定实际使用的对象,并且可能有几个不同的分支,以根据类型显示不同的内容.在小数点,日期时间等方面也需要考虑For.Metadata.DataFormatString
.
Writing a DisplayTagHelper
would be similar in concept, but a bit more difficult. You can get the value from For.Model
. However, this is typed as object
. Using information available on For.Metadata
, you'd need to determine what you're actually working with and probably have a few different branches to display things differently depending on the type. There's also For.Metadata.DataFormatString
that would need to be taken into account with things like decimals, DateTimes, etc.
这篇关于为什么Razor页面脚手架将HTML帮助器(而不是标签帮助器)用于索引,详细信息和删除页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!