Blazor OnPost***Async not triggering

I’m working on a Blazor server Project and as I’m new to this tech I’m facing an issue that I can’t understand.

In a nutshell, I have a small formula to add an object into my postgresDB. This formular present in my page.cshtml is supposed to trigger the method OnPostAddAsync() in the file Page.cshtml.cs.

Turns out, this methods is never triggered. I’m so lost I can’t even explain more my issue as I have the feeling that I’ve check every tutorials on the web regarding this issue.

I hope you’ll be able to put me back on tracks.

Here’s a piece of my page.cshtml

@page "/"
@model BlazorSrvPoc.Pages.IndexModel
@using BlazorSrvPoc.Data
@using BlazorSrvPoc.Services
@inject ApplicationDbContext DbContext
@inject RiskService RiskService
@inject NavigateService Navigate

<form asp-page-handler="Add" method="post">
    @Html.AntiForgeryToken()
    <div>
        <label for="reference">Référence:</label>
        <input id="reference" name="NewRisk.Reference" class="form-control" value="@Model.NewRisk.Reference" />
    </div>
    <div>
        <label for="status">Statut:</label>
        <input id="status" name="NewRisk.Status" class="form-control" type="number" value="@Model.NewRisk.Status" />
    </div>
    <div>
        <label for="author">Auteur:</label>
        <input id="author" name="NewRisk.Author" class="form-control" value="@Model.NewRisk.Author" />
    </div>
    <div>
        <label for="dateIdentified">Date Identifiée:</label>
        <input id="dateIdentified" name="NewRisk.DateIdentified" class="form-control" type="date" value="@Model.NewRisk.DateIdentified.ToString("yyyy-MM-dd")" />
    </div>
    <div>
        <label for="title">Titre:</label>
        <input id="title" name="NewRisk.Title" class="form-control" value="@Model.NewRisk.Title" />
    </div>
    <button type="submit" class="btn btn-primary">Ajouter</button>
</form>

A piece of my Page.cshtml.cs

using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using BlazorSrvPoc.Data;
using BlazorSrvPoc.Models;
using BlazorSrvPoc.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace BlazorSrvPoc.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ApplicationDbContext _dbContext;
        private readonly RiskService _riskService;
        private readonly ILogger<IndexModel> _logger;

        public IList<Risk> Risks { get; private set; }
        [BindProperty]
        public Risk NewRisk { get; set; }
        public int NbRisk;

        public IndexModel(ApplicationDbContext dbContext, RiskService riskService, ILogger<IndexModel> logger)
        {
            _dbContext = dbContext;
            _riskService = riskService;
            NewRisk = new Risk();
            _logger = logger;
        }

        public async Task OnGetAsync()
        {
            Risks = await _dbContext.Risks.ToListAsync();
        }

        public async Task<IActionResult> OnPostAddAsync()
        {
            _logger.LogInformation("Starting OnPostAddRiskAsync method");
            _logger.LogInformation("New Risk data: {Reference}, {Status}, {Author}", NewRisk.Reference, NewRisk.Status, NewRisk.Author);

            _logger.LogCritical("Model State: {IsValid}", ModelState.IsValid);
            if (!ModelState.IsValid)
            {
                Console.WriteLine("New Risk is not valid");
                return Page();
            }

            try
            {
                _dbContext.Risks.Add(NewRisk);
                await _dbContext.SaveChangesAsync();
                Console.WriteLine("Risque créé :");
                _logger.LogInformation("Number of risks after add: {Count}", Risks.Count);
                Risks = await _dbContext.Risks.ToListAsync();
                return Page();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                _logger.LogError(ex, "Error occurred while adding risk");
                return Page();
            }
        }
    }
}

In case, my Risk.cs model

namespace BlazorSrvPoc.Models
{
    public class Risk
    {
        public int Id { get; set; }
        public string Reference { get; set; }
        public int Status { get; set; }

        public string Title { get; set; }
        public DateTime DateIdentified { get; set; }
        public string Author { get; set; }

        // Relations one-to-one
        public RiskDescription Description { get; set; }
        public RiskAnalysis Analysis { get; set; }
        public RiskEvaluation Evaluation { get; set; }

        // Relation one-to-many
        public List<RiskTask> StateTasks { get; set; }
    }
}

There are potentials useless bits of code at the moment as I’ve tried several thing to be able to see what’s happening. at the moment, the page is rendered with the list of risk already in the DB, as soon as I click to add the object, the page is re-rendered with no data (which I think I understand why) but nothing else is happening. Nothing on the DB no logs what so ever, only the HTTP OK in the Chrome console.

Let me know if I can show you something else.

4

Add Taghelper in your page can fix the issue.

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Full Code

@page "/"
@model BlazorSrvPoc.Pages.IndexModel
@using BlazorSrvPoc.Data
@using BlazorSrvPoc.Services
@inject ApplicationDbContext DbContext
@inject RiskService RiskService
@inject NavigateService Navigate
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<form asp-page-handler="Add" method="post">
    @Html.AntiForgeryToken()
    <div>
        <label for="reference">Référence:</label>
        <input id="reference" name="NewRisk.Reference" class="form-control" value="@Model.NewRisk.Reference" />
    </div>
    <div>
        <label for="status">Statut:</label>
        <input id="status" name="NewRisk.Status" class="form-control" type="number" value="@Model.NewRisk.Status" />
    </div>
    <div>
        <label for="author">Auteur:</label>
        <input id="author" name="NewRisk.Author" class="form-control" value="@Model.NewRisk.Author" />
    </div>
    <div>
        <label for="dateIdentified">Date Identifiée:</label>
        <input id="dateIdentified" name="NewRisk.DateIdentified" class="form-control" type="date" value="@Model.NewRisk.DateIdentified.ToString("yyyy-MM-dd")" />
    </div>
    <div>
        <label for="title">Titre:</label>
        <input id="title" name="NewRisk.Title" class="form-control" value="@Model.NewRisk.Title" />
    </div>
    <button type="submit" class="btn btn-primary">Ajouter</button>
</form>

Explanation

  1. Razor Page and Razor Component are different. You can learn more about their differences.

  2. Blazor projects do not support TagHelper by default. If you add a razor page, you can add it manually as shown in the above code, and then you can run.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật