ASP NET Core 3 1 Build a Simple Razor Pages ToDo Application Part 10





Code: Category.cshtml



@page

@model ASPNETCoreWebApplication.Pages.CategoryModel

@{

    Layout = null;

}



<!DOCTYPE html>



<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />

    <title>Category</title>

</head>

<body>

    @foreach (var item in Model.Items)

    {

        <div class="bg-light card card-text font-weight-bolder font-italic"> ToDoItem : @item.Name</div> <br />

    }

</body>

</html>

Category.cshtml.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using ASPNETCoreWebApplication.Model;

using ASPNETCoreWebApplication.Services;

using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Mvc.RazorPages;



namespace ASPNETCoreWebApplication.Pages

{

    public class CategoryModel : PageModel

    {

        private readonly ToDoService _service;

        public CategoryModel(ToDoService service)

        {

            _service = service;

        }

        public ActionResult OnGet(string category)

        {

            category = "todolist";

            Items = _service.GetItemsForCategory(category);

            return Page();



        }

        public List<ToDoListModel> Items { get; set; }

    }

}

ToDoListModel.cs:



namespace ASPNETCoreWebApplication.Model

{

    public class ToDoListModel

    {

        public int Id { get; set; }

        public string Name { get; set; }

    }

}

ToDoService.cs

using ASPNETCoreWebApplication.Model;

using System.Collections.Generic;





namespace ASPNETCoreWebApplication.Services

{

    public class ToDoService

    {

        List<ToDoListModel> toDoLists = new List<ToDoListModel>();



        public List<ToDoListModel> GetItemsForCategory(string category)

        {

            if(category!=null)

            {

                toDoLists.Add(new ToDoListModel { Id = 1, Name = "Homework" });

                toDoLists.Add(new ToDoListModel { Id = 2, Name = "Do dishes" });

                toDoLists.Add(new ToDoListModel { Id = 3, Name = "Visit Market" });

            }

            return toDoLists;

        }

    }



Startup.cs

using ASPNETCoreWebApplication.Services;

using Microsoft.AspNetCore.Builder;

using Microsoft.AspNetCore.Hosting;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Hosting;



namespace ASPNETCoreWebApplication

{

    public class Startup

    {

        public void ConfigureServices(IServiceCollection services)

        {

            services.AddRazorPages();

            services.AddTransient<ToDoService>();

           

        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

        {

            //app.UseWelcomePage("/");

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }

            else

            {

                app.UseExceptionHandler

                (

                "/Error"

                );

            }

            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints

            (

               endpoints => {

                   endpoints.MapRazorPages();

               });

        }

    }

}

Comments

Popular posts from this blog

How to Fix "JsonReaderException: '}' is invalid after a single JSON valu...

ASP NET Core 3 1 Build a Complete MVC ToDo List Application Part 11

A Simple ML.NET Linear Regression Machine Learning Application