How to create Models In Asp.Net Blazor

In this article you will understand How to create Models In Asp.Net Blazor By Sagar Jaybhay.

In Asp.Net Razor pages we don’t have models folder now to perform CRUD operation we will create the Model Class Library Project in our application. By creating a class library project it is easily used in any other project like web API, asp.net mvc.

Models In Asp.Net Razor Pages:

public class Employee
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Photopath { get; set; }
        public Department? Dept { get; set; }

    }

    public enum Department
    {
        IT, HR, Support, Testing, Account
    }
Models In Razor
Models In Razor

In our, we added this.NetStandard class library projects with Employee as class and Department as an enum.

Now we will create a DataAccess Layer in our application to do this we again create .Net standard Class library project. In this, we create one Interface which has the GetAllEmployees method and which returns a list of employees. After that, we create the DbRepository class which implements the Interface. We create this for use of Dependency injection and Inversion of control and to know more about this pattern use the below link. https://sagarjaybhay.com/repository-pattern-asp-net-core-by-sagar-jaybhay/

Now we refer these projects in our main application and to check everything working fine we create the Employee field in our Index page and constructor we inject the IEmployeeRepos object by using dependency injection. Now we have to check the count of an employee. So in the Page Display template, we use the below code.

<h3>No of Employees : @Model.Employees.Count()</h3>

And our page model class looks like below

  public class IndexModel : PageModel
    {
        public List<RazorPages.Models.Employee> Employees;
        public IndexModel(IEmployeeRepos employeeRepos)
        {
            EmployeeRepos = employeeRepos;
        }

        public IEmployeeRepos EmployeeRepos { get; }

        public void OnGet()
        {
            Employees = EmployeeRepos.GetAllEmployees().ToList();          
        }
    }

After running this application we get below error output.

An unhandled exception occurred while processing the request.

InvalidOperationException: Unable to resolve service for type ‘DAL.IEmployeeRepos’ while attempting to activate ‘RazorApplication.Pages.Employee.IndexModel’.

Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

To resolve this error we need to do the following changes in our application. We forget to register our service that’s why this error occurred.  The error occurred because we never initialize Interface object with concrete type in our startup class configure service method. It means that below is code for registering the service in the Configure method.

To initialize this use the below code of configure service method

public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddSingleton<IEmployeeRepos, DBRepository>();
        }

In the above code, we use the AddSingleton method what is this? To learn about this visit this link to understand better https://sagarjaybhay.com/what-is-the-difference-between-addtransient-vs-addsingleton-vs-addscoped-in-asp-net-core-mvc-by-sagar-jaybhay/

The output looks like below

GitHub Link:- https://github.com/Sagar-Jaybhay/AspNetRazor

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Related posts