在ASP.NET Core MVC中使用标签助手的优势是什么 [英] What is the advantage of using Tag Helpers in ASP.NET Core MVC

查看:114
本文介绍了在ASP.NET Core MVC中使用标签助手的优势是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚好写了一篇很好的文章,介绍了称为

I just come across a good write up for a new ASP.NET Core feature called Tag helpers.

从那里开始,我了解到可以替换以下代码:

From there, I understood that one can replace the following code:

@model MyProject.Models.Product

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(m => p.Name, "Name:")
        @Html.TextBoxFor(m => p.Name)
    </div>
    <input type="submit" value="Create" />
}

具有:

@model MyProject.Models.Product
@addtaghelper "Microsoft.AspNet.Mvc.TagHelpers"

<form asp-controller="Products" asp-action="Create" method="post">
    <div>
        <label asp-for="Name">Name:</label>
        <input asp-for="Name" />
    </div>

    <input type="submit" value="Save" />
</form>

有一些新语法,例如asp-controllerasp-for等.但是它有什么作用?这种新方法的优点是什么?

There's some new syntax such as asp-controller, asp-for, etc. But what does it do? And what's the advantage of this new approach?

推荐答案

到目前为止,我看到的最重要的改进是它可以保证对HTML元素的控制.虽然方便,但MVC使用的HTML帮助程序会在您尝试执行并非为它们创建的操作时产生问题.

The most important improvement I've seen so far is the control it guarantees over your HTML elements. While convenient, the Html helpers used by MVC create problems when you try to do things they weren't built for.

在MVC5中使用TextBox时,可以看到一个简单的示例:

A simple example can be seen when using the TextBox in MVC5:

 @Html.TextBoxFor(m => p.Name)

生成的HTML标记如下:

The resulting HTML markup looks like:

<input class="form-control" id="Name" name="Name" type="text" value="">

又好又简单.但是,如果您想添加一个占位符属性,该怎么办?如果要使用引导程序的验证状态怎么办?如果您有一些需要自定义属性的第三方超酷javascript库,该怎么办.这些东西在 MVC5的初始版本中都是不可能的.尽管它们最终是通过更新以 htmlAttributes 的形式添加的.即使是现在,添加自定义属性充其量也是很麻烦的.

Nice and simple. But what if you want to add a placeholder attribute? What if you want to use bootstrap's validation states? What if you have some 3rd party super cool javascript library which needs custom attributes. None of these things were possible in the initial release of MVC5. Though they were eventually added via update in the form of htmlAttributes. Even now adding custom attributes is kludgey at best.

@Html.TextBoxFor(m => p.Name, 
    new {@class="form-control has-error", placeholder="Enter Name", 
    superCoolFeature="Do something cool"})

尽管您可以说这仍然比纯HTML的代码少,但它不再是一个明显的优势.更糟糕的是,该解决方案仍然无法覆盖相当普遍的属性中的破折号.如果需要它们,则可以使用诸如 ActionLink htmlAttributes

While you could argue this is still less code that straight HTML, it is no longer a significant advantage. Worse, this solution still doesn't cover dashes in attributes which are fairly common. If you need them you are stuck with a workaround such as ActionLink htmlAttributes

我已经走了使用自定义编辑器修复这些缺陷的路线,并尝试构建自己的TextBox控件.很快就很明显,替换包含的TextBox模板将需要大量的工作.更糟糕的是,您的模板必须知道要添加的任何扩展名才能使用它们.

I've gone down the route of fixing these deficiencies with custom editors, and tried building my own TextBox controls. It became obvious pretty quickly that replacing the included TextBox templates would require a lot of work. Worse, your templates have to have knowledge of any extensions you are adding to use them.

似乎在MVC中包含了Bootstrap和其他第三者工具,这使得当前设计在扩展HTML方面存在问题(需要修复)更加明显.希望标签助手的实现足够完整,以至于我们将来可以避免使用它们.

It seems like the inclusion of Bootstrap and other 3rd party tools into MVC have made it more obvious that the current design has problems with extending HTML which need to be fixed. Hopefully the tag helpers implementation is complete enough that we can avoid them in the future.

这篇关于在ASP.NET Core MVC中使用标签助手的优势是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆