✅ Calling toast library function in scaffolded cshtml.cs

Hi, Can someome please point on to how to use shared/mycomponent function in a scaffolded cshtml -> cs for example in identity/pages/account/manage/register.cshtml.cs i want to invoke my toast component https://github.com/Blazored/Toast
@inject IToastService toastService

@code {
public void Toast(string text)
{
RenderFragment message =@<text>@text</text>;
toastService.ShowToast(ToastLevel.Info, message);
Console.WriteLine("Not right!");
}
}
@inject IToastService toastService

@code {
public void Toast(string text)
{
RenderFragment message =@<text>@text</text>;
toastService.ShowToast(ToastLevel.Info, message);
Console.WriteLine("Not right!");
}
}
GitHub
GitHub - Blazored/Toast: A JavaScript free toast library for Blazor...
A JavaScript free toast library for Blazor and Razor Component applications - GitHub - Blazored/Toast: A JavaScript free toast library for Blazor and Razor Component applications
30 Replies
Mayor McCheese
Are you building a blazor app?
VitalJeevanjot
VitalJeevanjotOP2y ago
Yes Mayor Blazor-server one I am js developer trying my hands on blazor
Mayor McCheese
So.... did you read the docs on it? The author does a better job explaining than anyone else will and has samples.
VitalJeevanjot
VitalJeevanjotOP2y ago
Yes, i do. It works on components easily. But not when called from cshtml.cs file When i call the function from identity scaffolded register file, I get Object reference is not set an instance.... Then i try to use the component in cshtml file hoping this will initiaze the service but it doesn't refer it's like the builder references used with identity scaffolded or any scaffolded files is managed somewhere differently i am not getting the gist of it.
VitalJeevanjot
VitalJeevanjotOP2y ago
VitalJeevanjot
VitalJeevanjotOP2y ago
From where i need to call. Or to where i need to show the toast on some codition before adding user in db But since the serivce when i try to intialize there directly gives same error i tried to create this under a razor component The function you see there above
VitalJeevanjot
VitalJeevanjotOP2y ago
VitalJeevanjot
VitalJeevanjotOP2y ago
now using that inside another component will work as author mentioned but not when called in/from cshtml.cs file from identity
Mayor McCheese
I'm not certain of your solution right now, I feel like the first image you're showing is a regular mvc app, it's won't work there.
VitalJeevanjot
VitalJeevanjotOP2y ago
it is same solution. same project ooooooo i missed it
Mayor McCheese
I built my own toast service in wasm a while before this one came out in the initial preview, we eventually swapped out my solution for this one and had no issues
VitalJeevanjot
VitalJeevanjotOP2y ago
just saw in my own screen shot 😄 sounds good i will try out with something i just saw
Mayor McCheese
Saw what?
VitalJeevanjot
VitalJeevanjotOP2y ago
VitalJeevanjot
VitalJeevanjotOP2y ago
there is separate imports for this.... i thought all was done my main _imports but this was a view
Mayor McCheese
Hmmm I've not got a lot experience in blazor any longer it seems, mostly from the initial preview, I'm probably too out of touch with it
VitalJeevanjot
VitalJeevanjotOP2y ago
its fine, i myself starting it out just now thank you Mr. Mayor
VitalJeevanjot
VitalJeevanjotOP2y ago
@.mayormccheese Still not working In Register.cshtml.cs
VitalJeevanjot
VitalJeevanjotOP2y ago
There is no error by toast doesn't appear is that possible that css is having some sort of conflic ?
VitalJeevanjot
VitalJeevanjotOP2y ago
the component is declared in Register.cshtml
Mayor McCheese
If you check in the page is it rendering the html?
VitalJeevanjot
VitalJeevanjotOP2y ago
it doesn't. I checked on components it renders but through cshtml it doesn't OnPostAsync the page reloads itself when i return null so maybe that's why it re renders the dom but i was expecting even a glimpse There is one more thing cshtml.cs renders on backend and cshtml works as front-end but i didn't found a way to tringger @function{void toastmaker(){}} from cshtml.cs file in cshtml file to see if it works from front-end file
VitalJeevanjot
VitalJeevanjotOP2y ago
VitalJeevanjot
VitalJeevanjotOP2y ago
It shows fired by razor components as prerendered state should work live on server but the thing with cshtml and cshtml.cs is a whole different process i think The code should have written in front-end but they wrote it in backend file. I do not know how to trigger front-end changes through backend file as ajax process
Mayor McCheese
They are different processes, so you're going to have trouble and essentially maintain two components ( I think ) I don't know much about blazor auth; when I had to do auth I had to hand-roll oauth for blazor since the components didn't exist yet.
Mayor McCheese
GitHub
blazor-workshop/save-points/06-authentication-and-authorization/Bla...
Blazor workshop. Contribute to dotnet-presentations/blazor-workshop development by creating an account on GitHub.
Mayor McCheese
it's not the database stuff you have, but should be somewhat plug and play this is also wasm, which is somewhat identical
VitalJeevanjot
VitalJeevanjotOP2y ago
Thanks @.mayormccheese I have completed this workshop some weeks ago to get basics of it. Specially because it was from steve-sanderson so i belived it will be more clear and informative so i went through it and was good indeed. But thank you for you time. I will keep on trying.... Otherwise i will switch back to my own stack or probably go with mimal api don't know what to decide now
Mayor McCheese
Others may have more info, I answered because I dealt with this particular component a lot, but not in your cirucmstances unfortunately.
VitalJeevanjot
VitalJeevanjotOP2y ago
Still thanks

Did you find this page helpful?