MVC 5 with examples. Use of Inline HTML helper in

Also, we will learn about the Inline HTML helper in

In this article, you will learn the following points about HTML Helpers

Html Helpers are C# methods then why did they call them helpers?

How to reuse the same Inline Html Helper method in different Div?

How to use Inline HTML helpers on View?.

What is Inline HTML Helper in MVC 5?

What are HTML Helpers in ASP.NET MVC 5?

Following are the previews

articles on ASP.NET MVC

5

What is HTML Helper in ASP.NET MVC 5?





HTML Helpers are methods that return a string.

Helper class can create HTML controls programmatically. HTML Helpers are used in View to render HTML content.

It is not mandatory to use HTML Helper classes for building an ASP.NET MVC application.

We can build an ASP.NET MVC application without using them, but HTML Helpers helps in the rapid development of a view.

HTML Helpers are more lightweight as compared to ASP.NET Web Form controls as they do not use ViewState and do not have event models.

MVC has built-in Helpers methods

We can create custom HTML helpers.

Type of HTML Helpers?





HTML Helpers are categorized into three types:

Inline HTML helpers Built-in HTML helpers Standard HTML Helpers Strongly Typed HTML helpers Templated HTML helpers Custom HTML helpers

In this article, we will learn about Inline Html Helper in MVC 5 and other types we will discuss in the next subsequent article of this series.





What is Inline HTML Helper in MVC 5?

Inline Html Helper is used to create reusable Helper method by using the Razor @helper tag.

Inline helpers can be reused only on the same view.

We can not use Inline helper to the different view Pages.

We can create our own Inline helper method based on our requirements.





Advantages of using Inline HTML Helper in MVC 5:

It is reusable on the same view.

It reduces the code repetition

It is simple and easy to create.

It is easy to customization the method based on the requirement.

Syntax:

@helper HelperName(Parameters list..) { // code..... }









Example of Inline HTML helpers:

Example 1:

We can create inline Html Helper method having integer parameter.

@helper AddHelper( int a, int b) { < label > Addition of two No = @( a+b ) </ label > }





How to use inline Html Helper method in View.

< div style =" background-color : azure ; "> < label > @ AddHelper(100,200) </ label > </ div >





Output:









Example 2:

We can create " PrintHelper() " inline Html Helper method having string parameter.

@helper PrintHelper( string message) { < label > @ message </ label > }





How to use " PrintHelper() " inline Html Helper method in View.

< div style =" background-color : azure ; "> < label > @ PrintHelper( "Welcome to Learning ASP.NET MVC 5 Tutorials Step by Step." ) </ label > </ div >





Output 2:

Example 3:

We can create “ ListHelper () ” inline Html Helper method having a list as a parameter.

@helper ListHelper( string [] strList) { < ol > @ foreach (var item in strList) { < li > @ item </ li > } </ ol > }





How to use “ ListHelper () ” inline Html Helper method in View. Declare one string array as follow. After declaring string array use the Inline Html Helper method in View and pass string array to “ListHelper()” method.

@{ string [] strBooks = new string [] { "C#.NET" , "ASP.NET MVC" , "ASP.NET CORE" , "VB.NET" , "WEB API" }; }





Print the book list using ListHelper Inline Html Helper in View as follow.

< div id ="div1" style =" background-color : yellow ; "> Book Name List: @ ListHelper(strBooks) </ div >





Output 3:

Example 4:

How to reuse the Inline Html Helper method in the same view





We can declare one string array for list of a programming language as follow.

@{ string [] strBooks = new string [] { "C#.NET" , "ASP.NET MVC" , "ASP.NET CORE" , "VB.NET" , "WEB API" }; string [] strLanguages = new string [] { "C#.NET" , "VB.NET" , "F#.NET" , "JAVASCRIPT" }; }





How to reuse same “ ListHelper () ” inline Html Helper method in View.

< div id ="div1" style =" background-color : yellow ; "> Book Name List: @ ListHelper(strBooks) </ div > < hr /> < div id ="div2" style =" background-color : azure ; "> Programming Languages: @ ListHelper(strLanguages) </ div >





Output 4:





If you see on the above example you will get an idea about the Inline Html Helper method we have used ListHelper() method in div1 and div2 on the same view page. it reduces the code repetition and complicit. We can use the same helper method multiple times in the same view.





Html Helpers are C# methods then why did they call them helpers?

Because they all generate Html type of element which returns type is “MvcHtmlString” and It is a type of “HtmlHelper” class. All the Html c# extension methods are inside the “HtmlHelper” class. And the people remember them easily.





Example:

public static MvcHtmlString TextBox( this HtmlHelper htmlHelper, string name);









Interview Question & Home Work





Interview Question:

What is HTML Helper in Asp.NET MVC/MVC 5?

Use of Inline HTML Helpers in Asp.NET MVC/MVC 5?

Html Helpers are C# methods then why did they call them helpers?





HomeWork:

Create inline HTML helper for list of Students.

@helper StudentListHelper(string[] str) {// Code...........}









Related Articles:





References:









I hope you understand the concepts of HTML Helpers in ASP.NET MVC 5 . If you like this article then please share this article that will help other people to increase there knowledge.





Thanks for reading.





Don't Forget To :





More detail watch follow video on HTML Helpers In ASP.NET MVC 5:











