❔ The name "onclick" does not exist in the current context

I have
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web
at the top and
<button class="btn btn-secondary" @onclick="e => HandleSubmit(e)">Sign Up</button>
<button class="btn btn-secondary" @onclick="e => HandleSubmit(e)">Sign Up</button>
below.
12 Replies
sea cryptid
sea cryptidOP2y ago
It's working in this other file
sea cryptid
sea cryptidOP2y ago
It says in the file not working that the directive is not necessary if that helps.
Angius
Angius2y ago
Just to make sure, it's a .razor file?
sea cryptid
sea cryptidOP2y ago
lol well that must be the issue no it's cshtml
Angius
Angius2y ago
Well, that's the issue
sea cryptid
sea cryptidOP2y ago
Yep. Working example is razor OK thank you.
Angius
Angius2y ago
Only Blazor components have that @onclick thing And other Blazor-specific tbings
sea cryptid
sea cryptidOP2y ago
So what is the best practice here? I guess I should have my cshtml page just have a razor component? Also wondering if I should make a form component for stuff like
<form method="post">
<div class="form-group">
<label for="inputEmail">Email</label>
<input type="email" class="form-control" id="inputEmail" aria placeholder="[email protected]">
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="quote" class="form-control" id="inputPassword">
</div>
<button class="btn btn-secondary" @onclick="e => HandleSubmit(e)">Sign Up</button>
</form>
<form method="post">
<div class="form-group">
<label for="inputEmail">Email</label>
<input type="email" class="form-control" id="inputEmail" aria placeholder="[email protected]">
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="quote" class="form-control" id="inputPassword">
</div>
<button class="btn btn-secondary" @onclick="e => HandleSubmit(e)">Sign Up</button>
</form>
So that I can just do a foreach on something like
["email","password"]
["email","password"]
or whatever.
Angius
Angius2y ago
If you're using pure Blazor, just don't have .cshtml files anywhere If you're doing something mixed, yeah, reusable components are the way to go
sea cryptid
sea cryptidOP2y ago
Oh. The tutorial I followed earlier had cshtml pages in a "Pages" directory than then call blazor components with
@(await Html.RenderComponentAsync<Login>(RenderMode.ServerPrerendered))
@(await Html.RenderComponentAsync<Login>(RenderMode.ServerPrerendered))
I would be surprised if this is bad practice because it was a tutorial from Microsoft but should I just be routing directly to Componenets? Although it is possible that for saving time they did not do things the "optimal" way. Wait . . . Should I be only putting razor pages in the Pages directory and then from there call razor components?
Angius
Angius2y ago
You're not routing to components, you're embedding them, placing them on the page Razor Pages should be in the /Pages dir, yes
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server