Blazor vs JavaScript Sample Code 01

2019-05-07


The famous Blazor demo application provides the case of clicking a button to increment a counter.

blazor click button 1

Let's implement it using Blazor code and JavaScript.


Blazor

@page "/counter"

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" onclick="@IncrementCount">Click me</button>

@functions {
    int currentCount = 0;

    void IncrementCount()
    {
        currentCount++;
    }
}

JavaScript

<h1>Counter</h1>

<script>
    var currentCount = 0;

    function IncrementCount() 
    {
        document.getElementById('mycnt').innerHTML  =  currentCount++;
    }
</script>

<p>my count2: <span id="mycnt"></span></p>

<button class="btn btn-primary" onclick="IncrementCount()">Click me</button>