TOC

The community is working on translating this tutorial into Persian, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Razor:

Local Functions

In a previous article, we talked about how Razor allows you to define a block of code inside your Views, like this:

@{
    // C# code goes here...
    // As many lines as you want!
}

You can use these code blocks for pretty much anything that C# can do - you can even define local functions! This is really cool, because a local Razor function allows you to include markup, which makes it possible to create small template functions. This is useful in a number of situations, for instance as we can see in this next example, where we need to generate the same piece of markup multiple times. Defining a local function is just like defining a class method, except a local function doesn't have an access modifier (public, private etc.). Here's how it could look:

@{
    void RenderUserInfo(HelloMVCWorld.Models.WebUser user)
    {
<div class="user-info">
    @user.LastName, @user.FirstName [@user.Birthday.ToShortDateString()]
</div>
    }
}

Notice how elegantly Razor allows you to mix C# code and HTML. This example is kept very simple, markup-wise, on purpose, but it can be as complicated as you need it to be!

Here's a complete example on how you can define and use this local function:

@model List<HelloMVCWorld.Models.WebUser>
@{
    ViewData["Title"] = "Functions";
}

<h1>Functions</h1>

@{
    void RenderUserInfo(HelloMVCWorld.Models.WebUser user)
    {
<div class="user-info">
    @user.LastName, @user.FirstName [@user.Birthday.ToShortDateString()]
</div>
    }

    <h2>Users [sorted by first name]</h2>
    foreach(var user in Model.OrderBy(x => x.FirstName))
    {
RenderUserInfo(user);
    }

    <h2>Users [sorted by last name]</h2>
    foreach(var user in Model.OrderBy(x => x.LastName))
    {
RenderUserInfo(user);
    }
}

Thanks to the local function (RenderUserInfo), we can now output the same list two times, with different sorting and without having to write the same markup twice!

Local functions with a return value

You will notice that the RenderUserInfo() function doesn't return anything (it's declared with void as the return value), but it doesn't have to be that way. Your local Razor functions can easily return a value, just like a regular C# function/method. This can be useful in some situations, e.g. when you want to do a complicated calculation from multiple places in your code. Here's an example:

@{ 
    int GetTheAnswerToLifeTheUniverseEverything(int x, int y)
    {
return x * y;
    }
}
<div>
    The answer to life, the universe and everything: @GetTheAnswerToLifeTheUniverseEverything(7, 6)
</div>

Now obviously this is just a silly example, so normally the function would do something more interesting and complicated than a simple multiplication, but it should demonstrate how easy it is to define and use a local function with a return value. The output will look like this:

<div>
    The answer to life, the universe and everything: 42
</div>

Summary

Local Razor functions allows you to easily define functions which can be used in the current View, e.g. to perform the same action multiple times or to be used as a template.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!