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
-
Razor Page and Razor Component are different. You can learn more about their differences.
-
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.