Blazor is a .NET web framework for building the client-side application with c#, Razor syntax, and HTML that runs in the browser. Blazor is a web framework to build an easy way to build a web application. It runs on any browser that supports the web assembly, it is fast, open-source, and have reusable components. In this blog, we will learn about Blazor and how to do the event calling and data binding in the project. We need visual studio 17 or above version, and .NET core 3.1 or above.
Let’s start the project.
Open visual studio 2017 or latest, go to the File-> New-> Project-> .NET core web application-> Blazor.
Now we can see the already written pages like index, cou nter, and fetch data. On the counter page, there is a variable called the current counter. It will increase by one as shown below.
@page "/counter"
Current count: @currentCount
@code { private int currentCount = 0; private void IncrementCount() { currentCount += 1; } }In the above code, we will add the input box to bind the counter data. Add the input box and bind the data using the bind property.
@page "/date"
Pick the Date :
You have picked the date : @pickDate.ToShortDateString()
@code { DateTime pickDate; }In the above code, we have taken the date picker and displayed the date as we have selected in the date picker.
Figure 2.0 Date Picker
These are the simple example to bind the date and value. Let's see another example for the display book list.
Initially we will be going to add a razor component. Right-click the pages folder and click on the razor component. It will create the “. razor" page. And named it "Book. razor".
The page will look like this:
@page "/date"
Now add this page to the navbar. There is a page called “NavMenu.razor”. Add this below code.
Now add a class on the razor page for book details. Like book name, book description, book price. Paste below code in the “@code {}” block.
public class BookList { public string BookName { get; set; } public string BookDescription { get; set; } public int BookPrice { get; set; } }
Now we will display the list of books. Using a foreach loop we will have the name of the books as an unordered list.
@page "/book"
Now, what if we want to add the book to this book list. For that, we will create a small form. In which we will take three input elements as shown below.
@page "/book" BookList
- @foreach (var book in bookLists) {
- @book.BookName
- }
Now we can see here that we have added the button. On click of the button, it will call the "Add" method. Now we will execute code for it. Paste the below code for the add method.
@page "/book" BookList
- @foreach (var book in bookLists) {
- @book.BookName
- }
@code { private ListbookLists = new List (); private bool hide { get; set; } = true; private string newBook; private string description; private int price; void Add() { if (!string.IsNullOrWhiteSpace(newBook)) { bookLists.Add(new BookList { BookName = newBook, BookPrice = price, BookDescription = description }); newBook = string.Empty; description = string.Empty; price = 0; } } public class BookList { public string BookName { get; set; } public string BookDescription { get; set; } public int BookPrice { get; set; } } }
Now run the project
Figure 3.0 Add in book list.
Figure 4.0 Book List
It’s okay, it will work. But we want to display the whole list of books that we have added.
Now we can see here that we have added the button. On click of the button, it will call the "Add" method. Now we have to implement code for it. Paste the below code for the add method.
@foreach (var book in bookLists) { BookClicked(book)">@book.BookName }
Replace this code in for loop. Now create the method in the code.
void BookClicked(BookList value) { bookDetail = value.BookName; descriptionDetail = value.BookDescription; priceDetail = value.BookPrice; hide = !hide; }
This is called the event binding. The full code of the project looks like this:
@page "/book" BookList
- @foreach (var book in bookLists) {
- BookClicked(book)">@book.BookName
- }
@code { private ListBook Name : @bookDetail
Book Description : @descriptionDetail
Book Price : @priceDetail
bookLists = new List (); private bool hide { get; set; } = true; private string newBook; private string description; private int price; private string bookDetail; private string descriptionDetail; private int priceDetail; void Add() { if (!string.IsNullOrWhiteSpace(newBook)) { bookLists.Add(new BookList { BookName = newBook, BookPrice = price, BookDescription = description }); newBook = string.Empty; description = string.Empty; price = 0; } } void BookClicked(BookList value) { bookDetail = value.BookName; descriptionDetail = value.BookDescription; priceDetail = value.BookPrice; hide = !hide; } public class BookList { public string BookName { get; set; } public string BookDescription { get; set; } public int BookPrice { get; set; } } }
When you click on the list it will display the whole detail of the book that we have added.
Figure 5.0 Description
In this blog, we have seen how to create Blazor web apps, how to make a list, data binding, and event calling. When we write the "@bind" that means we are binding the data and when we write the "@onclick" that means we are calling the method or event. It is simple because there is no interaction with JavaScript or any scripting language. When you run the project for the first time then it will take some time to load because it downloads the assemblies on the browser.
July 29, 2021
July 22, 2021
July 20, 2021
July 16, 2021
Well do everything we can to make our next best project!
Check out our most recent blogs
July 29, 2021
What is Angular? Angular is a frontend development framework used for building single-page client applications using HTML and Typescript. It is written in Typescript. What...
July 22, 2021
What is a Compiler? A compiler is nothing but a part of code that converts one programming language to another. If we talk about some simple programming languages like C, C++,...
July 20, 2021
Introduction Angular is a remarkable framework that may be used to create mobile and desktop web apps with spectacular UIs. It's made with JavaScript. we can utilize HTML, CSS,...
Well do everything we can to make our next best project!