ASP.NET MVC Badge Overview

Telerik UI for ASP.NET MVC Ninja image

The Badge is part of Telerik UI for ASP.NET MVC, a professional grade UI library with 110+ components for building modern and feature-rich applications. To try it out sign up for a free 30-day trial.

The Telerik UI Badge HtmlHelper for ASP.NET MVC is a server-side wrapper for the Kendo UI Badge widget.

The Badge is an absolutely positioned element that is used to decorate avatars, navigation menus, or other components in the application when the visual notification is needed.

It also provides customizing its content through templates, setting different types and layouts.

Initializing the Badge

The following example demonstrates how to define the Badge.

    <a class="k-button k-button-solid-base k-button-solid k-button-md k-rounded-md">
        @(Html.Kendo().Badge()
            .Name("badge")
            .Text("42")
            .Rounded(Rounded.Small)
        )
    </a>

Basic Configuration

The badge also provides the choice to be inline or overlay and set its type. To make the badge overlay add the k-badge-overlay class to the parent parent element.

    <a class="k-button k-button-solid-base k-button-solid k-button-md k-rounded-md k-badge-overlay">
        @(Html.Kendo().Badge()
            .Name("badge")
            .Text("42")
            .ThemeColor(BadgeColor.Warning)
            .Rounded(Rounded.Large)
        )
    </a>

Using templates

With the badge you can customize the content using templates.

    <a class="k-button k-button-solid-base k-button-solid k-button-md k-rounded-md k-badge-overlay">
        @(Html.Kendo().Badge()
            .Name("badge")
            .Text("42")
            .Template("#= this._text > 10 ? '9+' : this._text #")
            .Rounded(Rounded.Full)
        )
    </a>

Using as a label

You can integrate the Badge into other UI components. The following example demonstrates how to use it as a label in Grid client column templates.

    @(Html.Kendo().Grid<OrderViewModel>()
        .Name("grid")
        .Columns(columns => {
            columns.Bound("OrderID").HtmlAttributes(new { @class = "templateCell" }).ClientTemplateId("orderTemplate");
        })
        .Events(ev => ev.DataBound("initBadges"))
        .DataSource(dataSource => dataSource
          .Ajax()
          .Read(read => read.Action("Orders_Read", "Grid"))
       )
    )

    <script type="kendo-template" id="orderTemplate">
        #if(OrderID <= 10){#
            @(Html.Kendo().Badge()
                .Name("flag#=OrderID#")
                .ThemeColor(BadgeColor.Success)
                .Text("New")
                .ToClientTemplate()
            )
        #}#
        #if(OrderID > 10){#
            @(Html.Kendo().Badge()
                .Name("flag#=OrderID#")
                .ThemeColor(BadgeColor.Error)
                .Text("Old")
                .ToClientTemplate()
            )
        #}#
    </script>
    function initBadges(e) {
        $(".templateCell").each(function(){
            eval($(this).children("script").last().html());
        });
    }

Referencing Existing Instances

You can access an existing Button instance by using the jQuery.data() method which gets executed by the jQuery object of the originating element. Once the reference is established, use the Badge client-side API to control its behavior.

    <a class="k-button k-badge-overlay">
    @(Html.Kendo().Badge()
        .Name("badge")
        .Text("42")
        .Template("#= this._text > 10 ? '9+' : this._text #")
        .Rounded(Rounded.Large)
    </a>

    <script>
        var badge = $('#badge').data('kendoBadge');
    </script>

See Also

In this article