https://www.cnblogs.com/caofangsheng/p/10462494.html
In this article, I will lead you to learn HTML Helper. [PS: Previous -- > 5.ASP. What is Area in net MVC]
HTML helpers are used to create HTML tags and then HTML controls. The HTML Helper is simply a method that returns an HTML string. ASP.NET MVC has three kinds of HTML Helpers:
1. Inline HTML Helper: it is mainly created by using the @ helper tag in Razor syntax. Inline HTML helpers can only be reused in the same view. If you want to use it in all views, there are ways to solve it. It will be introduced later.
2. Built in HTML Helper s: this kind of HTML helpers is the extension method of HtmlHelper class, which is further divided into three categories:
2.1 standard HTML Helper s: used to create the most commonly used HTML tags.
2.2 Strongly Typed HTML Helpers [Strongly Typed HTML Helpers]: this kind of HTML is generated through the attributes of the Model class and uses Lambda expressions to generate HTML.
2.3 Templated HTML Helpers [Templated HTML Helpers]: the HTML generated by this helper depends on the attributes of the Model class.
3. Custom HTML Helpers [Custom HTML Helpers]: you can create custom helper methods by using HtmlHelper extension methods or static methods in tool classes.
1. Let's take a look at the inline HTML Helper.
Create a project HTMLHelpersWithMVC, and create a Home controller and Index view:
View page:
Run it: [renderings]
Now, what if I have a Test page that also displays this? Let's write it directly on the Test page:
Look, the error is reported. The inline method can only be used on the declared view page. What should I do? We can do this:
Right click the item to create an App_Code folder:
In app_ Under the code folder, create a distribution view:
Then, get all the inline Html Helper method declarations on the Home controller Index page just now:
Then run the project: Look:
What's wrong? Now the Index page is also wrong. What's wrong???
Let's change it this way: put the app_ The properties of the view page under the code folder are changed to embedded resources and copied if it is newer
Then modify the view page as follows:
Then run to see the effect of two pages:
See this, you can realize [Inline HTML Helpers] Inline HTML Helpers on multiple pages.
2.1 now let's look at the standard HTML Helpers in the built-in HTML Helpers.
The operation effect is as follows:
2.2 now start to learn the built-in strong HTML type HTML Helpers of [build in HTML Helpers]:
Create a UserInfo class under the Models folder
To demonstrate this strongly typed, I create a new controller Account and create an Index view:
Running program:
2.3 now let's look at what templated HTML Helpers do:
In the Account controller, we add a Temp method and create a Temp view:
Run to Temp page:
Found @ HTML Editorformodel () automatically creates controls for us.
3. Finally, let's see how to create custom HTML Helpers
Create a Custom controller:
Create a CustomClass:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace HTMLHelpersWithMVC.Common { public static class CustomClass { /// <summary> ///Implementation of extension method -- create submit button /// </summary> ///< param name = "helper" > extension class object < / param > ///< param name = "name" > button name < / param > ///< param name = "value" > button value < / param > /// <returns></returns> public static MvcHtmlString CreateSubmit(this HtmlHelper helper, string name, string value) { var btn = "<input type='submit' name='"+name+"' value='"+value+"'/>"; return new MvcHtmlString(btn); } /// <summary> ///Static class implementation -- create submit button /// </summary> ///< param name = "name" > button name < / param > ///< param name = "value" > button value < / param > /// <returns></returns> public static MvcHtmlString CreateSubmit(string name, string value) { var btn = "<input type='submit' name='" + name + "' value='" + value + "'/>"; return new MvcHtmlString(btn); } } }
Note that this class, CustomClass, must be created in the program root directory before it can be clicked in the view [intelligent prompt]
correct:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace HTMLHelpersWithMVC { public static class CustomClass { /// <summary> ///Implementation of extension method -- create submit button /// </summary> ///< param name = "helper" > extension class object < / param > ///< param name = "name" > button name < / param > ///< param name = "value" > button value < / param > /// <returns></returns> public static MvcHtmlString CreateSubmit(this HtmlHelper helper, string name, string value) { string btn = "<input type='submit' name='" + name + "' value='" + value + "'/>"; return new MvcHtmlString(btn); } /// <summary> ///Static class implementation -- create submit button /// </summary> ///< param name = "name" > button name < / param > ///< param name = "value" > button value < / param > /// <returns></returns> public static MvcHtmlString CreateSubmit(string name, string value) { string btn = "<input type='submit' name='" + name + "' value='" + value + "'/>"; return new MvcHtmlString(btn); } } }
In the Index view of the Custom controller:
Running program:
Well, this article is over, ASP Net MVC HTML helpers, have you learned?