Learn C#.NET, ASP.NET MVC 5,ASP.NET Core that you can increase your knowledge and coding to develop the real-time project.


Sunday, September 22, 2019

HTML Helpers In MVC 5

We will learn HTML Helpers in MVC 5 and type of HTML Helpers in MVC 5. Also, we will learn about the Inline HTML helper in ASP.NET MVC 5 with examples. Use of Inline HTML helper in ASP.NET MVC 5.
html helpers in mvc 5

In this article, you will learn the following points about HTML Helpers in MVC 5.
  • What are HTML Helpers in ASP.NET MVC 5?
  • Type of HTML Helpers?
  • What is Inline HTML Helper in MVC 5?
  • How to use Inline HTML helpers on View?.
  • Example of Inline HTML helpers?
  • How to reuse the same Inline Html Helper method in different Div?
  • Html Helpers are C# methods then why did they call them helpers?
  • Interview Question & Home Work

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:
  1. Inline HTML helpers
  2. Built-in HTML helpers
    1. Standard HTML Helpers
    2. Strongly Typed HTML helpers
    3. Templated HTML helpers
  3. 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:


To understand the Inline Html Helper we need to create one application in visual studio 2017/2015/above.

In previous tutorials, we have understood about creating a project in ASP.NET MVC 5 using Visual Studio. How to add a controller and Add View in MVC 5.

 After creating the application we need to create on Index view And Add the following code.

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:
HTML Helpers In MVC 5



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:
HTML Helpers In MVC 5
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:
HTML Helpers In MVC 5
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:
HTML Helpers In MVC 5


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); 

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

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:



Share:

0 comments:

Post a Comment

Upcoming Articles/Videos

Design Pattern
SOLID Design Principles
Copyright © Programming With Shri | Powered by Shrimant Telgave Home | Disclaimer | Privacy Policy | Terms and Conditions Design by Shrimant Telgave