Answered by:
Problem with on Initialize() method

Question
-
User887623398 posted
Hello,
I'm trying to learn blazor from a tutorial.
When I write this statement in may page:
protected override void OnInit() { customers = customerManager.SelectAll(); }
and after that explains:
You are basically overriding OnInit() method of ComponentBase class (Blazor components inherit from ComponentBase class). Inside, you call SelectAll() method and store the return value into customers member.
I see red lines under OnInit( ) method. And visual studio suggests OnInitialize().
Is this method correct? So what are these red lines?
Should I add specific name space?
Saeed
Thursday, March 26, 2020 7:59 PM
Answers
-
User753101303 posted
I meant
protected override void OnInitialized() // was OnInit in an early preview and the name was changed since then { customers = customerManager.SelectAll(); }
From https://devblogs.microsoft.com/aspnet/asp-net-core-and-blazor-updates-in-net-core-3-0-preview-8/ : "In Razor components rename
OnInit
toOnInitialized
andOnInitAsync
toOnInitializedAsync"
Not sure if the turorial mention the exact version but it is outdated. I often prefer to favor the official documentation https://docs.microsoft.com/en-us/aspnet/core/tutorials/build-your-first-blazor-app?view=aspnetcore-3.1 and using tutorials with caution especially if no version at all is mentioned.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 27, 2020 11:47 AM
All replies
-
User753101303 posted
Hi,
Try OnInitialized instead. The tutorial you are using is based on an older preview version.
Thursday, March 26, 2020 8:34 PM -
User887623398 posted
All code cames here:
public class Customer { [Key] [Required] [StringLength(5, MinimumLength = 5)] public string CustomerID { get; set; } [Required] [StringLength(30)] public string CompanyName { get; set; } [Required] [StringLength(30)] public string ContactName { get; set; } [Required] [StringLength(15)] public string Country { get; set; } }
public class AppDbContext:DbContext { public AppDbContext( DbContextOptions<AppDbContext> options) : base(options) { } public DbSet<Customer> Customers { get; set; } }
public class CustomerManager { private AppDbContext db; public CustomerManager(AppDbContext db) { this.db = db; } public List<Customer> SelectAll() { return db.Customers.ToList(); } public Customer SelectByID(string id) { return db.Customers.Find(id); } public string Update(Customer obj) { db.Update(obj); db.SaveChanges(); return "Success!"; } }
Thursday, March 26, 2020 8:50 PM -
User753101303 posted
I meant
protected override void OnInitialized() // was OnInit in an early preview and the name was changed since then { customers = customerManager.SelectAll(); }
From https://devblogs.microsoft.com/aspnet/asp-net-core-and-blazor-updates-in-net-core-3-0-preview-8/ : "In Razor components rename
OnInit
toOnInitialized
andOnInitAsync
toOnInitializedAsync"
Not sure if the turorial mention the exact version but it is outdated. I often prefer to favor the official documentation https://docs.microsoft.com/en-us/aspnet/core/tutorials/build-your-first-blazor-app?view=aspnetcore-3.1 and using tutorials with caution especially if no version at all is mentioned.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 27, 2020 11:47 AM