gpt4 book ai didi

asp.net-core-mvc - Tag Helpers 与 Helper 类有何不同?

转载 作者:行者123 更新时间:2023-12-01 17:41:22 26 4
gpt4 key购买 nike

ASP.Net MVC 6 引入了标签助手功能。

为什么我们应该使用标签助手而不是以前版本提供的 Helper 类?

最佳答案

引入标签助手有几个原因。 Scott Hanselman talks in length about them here.但是,标记帮助程序的要点包括:

前端开发人员不需要 C# 知识来编写它们。

下面的内容对于传统前端开发人员来说确实不太直观。

@Html.LabelFor(m => m.Title, new { @class = "control-label" })
@Html.TextBoxFor(m => m.Title, new { @class = "form-control" })

但是,使用标签助手会变得更加清晰:

<label asp-for="Title" class="control-label" />
<input asp-for="Title" class="form-control" />

请注意,我上次检查时情况一直在变化,包括是否使用 asp- 前缀。

它可以更轻松地添加其他属性,例如 classplaceholder,甚至其他自定义属性。

我在我的项目中使用 angular.js;为此,语法变得更加困惑:

@Html.TextBoxFor(m => m.Title, new { @class = "form-control", ng_model = "ctrl.title" })

现在就像添加属性一样简单,就像我们期望的那样:

<input asp-for="Title" class="form-control" ng-model="ctrl.title" />

您甚至可以自己编写。

我不会在这里深入讨论,但您可以创建服务器端扩展小部件,甚至可以匹配您的客户端框架以实现 SEO 目的。有一个相当不错的tutorial for making tag helpers .

关于asp.net-core-mvc - Tag Helpers 与 Helper 类有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29839873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com