C
C#17mo ago
saberune

❔ Razor Page - Select Tag Helper (form-select): Need help with onchange event for drop-down list

In my APR.cshtml page I have the following drop down list present: <select asp-for="@Model.ServerName" class="form-select" onchange="Model.ChangeServer()"> The drop down-list works fine but I cannot find the right syntax for the onchange element. I've tried different variations of what's depicted above but non seem to work. For context, in the associated model page (APR.cshtml.cs) I have a function called ChangeServer which I want to change the values of a different drop-down list in my form when a different selection is made for the drop-down list above. Any guidance would be greatly appreciated. I feel like I'm missing something significant and I've googled a lot but haven't found the right resource 😦
9 Replies
Angius
Angius17mo ago
Just so we're clear: it's about Razor Pages with .cshtml and .cshtml.cs files, right? Not Blazor with .razor components? If so, then you can't just call the backend code from the frontend like that You'll either need to use Javascript to call some API controller, or you'll need to submit the data in a form
saberune
saberune17mo ago
Yea it's Razor Pages with .cshtml and .cshtml.cs files. The app is not MVC, it's just those two pages. I thought I could just call a function from the .cshtml.cs for the on change 😦 I guess a potential solution could be putting the drop-down list that I want to do on change for in a separate form so it can be submitted by itself when the on change happens? Is that possible?
Angius
Angius17mo ago
You'd still need detect the change with JS There's no functionality in HTML that'd send a form automatically when one of the fields changes
saberune
saberune17mo ago
oooh I see, that's probably why I couldn't find anything on what I was trying to do Thank you! I will look more into javascript @Angius So I tried using 2 separate forms one for each drop down list. The first form is basically the drop down list for server selection. I used the javascript call to submit the first form. The @Model.ServerName binds the value to the ServerName property in the .cshtml.cs, however my second form which processes that property when submitted now gives me a NullReferenceException when I try to use that property. My question is, do the properties in my models page only persist for each individual form? I'm just confused because it was working fine when everything was in one form. Thank you in advance for your advice!
Angius
Angius17mo ago
Every form sends only the data it contains
saberune
saberune17mo ago
Yes, but can the handler for a different form interact with the property sent from the first form? Or do you mean that each form's handler can only access the data passed by its own form?
Angius
Angius17mo ago
Each request is unique Each requests creates a new instance of the .cshtml.cs class
saberune
saberune17mo ago
OH I see okay that makes sense thanks. I thought it persisted almost like a storage
Accord
Accord17mo 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
More Posts