Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Form Tag Helpers in ASP.NET Core MVC

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use Form Tag Helpers in ASP.NET Core MVC, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Brief introduction

HTML or Web forms are used to collect input from users by using various HTML elements, such as input boxes, check boxes, radio buttons, drop-down lists, and so on. When using elements, we usually use POST requests. Although GET requests can also be used, GET requests are recommended to submit a small amount of non-sensitive data to obtain data.

ASP.NET Core MVC provides some Form Tag Helpers, so let's see what's most important.

Form Tag Helper

When we use elements, ASP.NET Core MVC automatically adds protection against CSRF attacks; it prevents CSRF attacks by generating hidden request verification tokens.

Look at a simple example:

The interface for the output of this code is as follows:

However, that's not all, what's going on behind the scenes:

Note that in HTTP Post operation, RequestVerificationToken works with the built-in [ValidateAntiForgeryToken] attribute, and the request method is marked as post.

Form Tag Helper also provides the asp-route- {ParameterName} attribute, where the parameter name is the value added to the route, and provides a clear alternative to @ Html.BeginForm and @ Html.BeginRouteForm.

If we add a custom route:

And modify the form to read as follows:

We get the following HTML:

It will execute the same controller and action methods as you just specified.

This is using the old HTML Helpers syntax:

@ using (Html.BeginForm ("Edit", "Categories")) {}

The new syntax is:

{}

The first may seem shorter and better, but the second is more natural, and it will be more natural for a designer or a JS developer to use Angular or React.

Input Tag Helper

This one is simple. It's an alternative to @ Html.EditorFor (m = > m.Name).

Suppose we have a view model with the Name property:

`

The following code is generated:

Label Tag Helper

This one is simple, too, as an alternative to @ Html.LabelFor (m = > m.Name).

It is used with the input box Tag Helper:

Like others, it has IntelliSense, and once you start typing, the name of the attribute is detected:

If we apply the Display attribute to the Name attribute:

This is the HTML content we will get:

Category NameTextArea Tag Helper

This one is very similar to Input Tag Helper:

The following Razor code:

`

We will get the following output:

Validation Message Tag Helper

We use Validation Tag Helper to display validation messages for the properties of our view model, and it is also simple to use:

The HTML we got:

Thank you for reading this article carefully. I hope the article "how to use Form Tag Helpers in ASP.NET Core MVC" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report