asp.net-mvc Create inline classes and methods using @functions


Example

Using Razor @functions keyword gives the capability of introducing classes and methods for inline use within a Razor file:

@functions
{
    string GetCssClass(Status status)
    {
        switch (status)
        {
            case Status.Success:
                return "alert-success";
            case Status.Info:
                return "alert-info";
            case Status.Warning:
                return "alert-warning";
            case Status.Danger:
            default:
                return "alert-danger";
         }
     }
}


<label class="alert @GetCssClass(status)"></label>

The same can be done for classes:

@functions
{
    class Helpers
    {
        //implementation
    }
}