Local Functions in Razor Views in Asp.net Core MVC

Local Functions in Razor Views in Asp.net Core MVC
Local functions are C# functions defined within the scope of a Razor code block (a code block enclosed in @{ … }). They allow you to encapsulate reusable logic directly in your views, making your view code more modular, organized, and easier to read.
 
Syntax and Features
Declaration: Local functions are declared using the standard C# method syntax, typically within an @functions { … } block.
Scope: They can only be called within the code block or section where they are defined.
Parameters and Return Types: Local functions can take parameters and return values, just like regular C# methods.
Accessibility: They are implicitly private to the view.
Access to View Data: Local functions can access variables and model properties declared within the same code block.
 
Why Use Local Functions?
Encapsulation: Group related logic into self-contained functions, improving code readability.
Readability: Break down complex code into smaller, more manageable chunks.
Reusability: Call local functions multiple times within the same view, avoiding code duplication.
Cleaner Views: Reduce the amount of inline C# code scattered throughout your HTML markup.
 
Code
@using ViewsExample.Models
 
@{
    string appTitle = “Asp.Net Core Demo App”;
    List<Person> people = new List<Person>()
    {
        // … (person data)
    };
}
 
@functions {
    double? GetAge(DateTime? dateOfBirth)
    {
        if (dateOfBirth is not null)
        {
            return Math.Round((DateTime.Now – dateOfBirth.Value).TotalDays / 365.25);
        }
        else
        {
            return null;
        }
    }
 
    int x = 10; // Example of a local variable
 
    string Name { get; set; } = “Hello name”; // Example of a local property
}
 
<!DOCTYPE html>
<html>
    <head>
        <title>@appTitle</title>
        <meta charset=”utf-8″ />
    </head>
    <body>
        <h1>Welcome to @Name</h1>
        @for (int i = 0; i < 2; i++)
        {
            Person person = people[i];
            <div>
                @person.Name
                <span>, </span>
                <span>@person.PersonGender</span>
                @if (person.DateOfBirth != null)
                {
                    <span>@person.DateOfBirth.Value.ToString(“MM/dd/yyyy”)</span>
                    <span>@GetAge(person.DateOfBirth) years old</span>
                }
            </div>
        }
    </body>
</html>
In this code:
Local Function: GetAge(DateTime? dateOfBirth) calculates the age of a person based on their date of birth.
Local Variable: int x = 10; This is not used in the view but demonstrates how to declare local variables within a Razor code block.
Local Property: string Name { get; set; } = “Hello name”; This is used to set the title of the page.
The GetAge function is called within the @foreach loop to display the age of each person if their date of birth is available.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply