Troubleshooting issue with RadioButton check change not updating in onPost event in ASP.Net project utilizing Bootstrap HTML and C

Having created two radio buttons, grouped them together, and applied bootstrap classes for styling; I am encountering an issue where the values of the radio buttons are not updating when clicked and submitted. Interestingly, the checkboxes on the same form are functioning correctly.

To represent the checked states of the checkboxes and radio buttons, I have created boolean properties. However, only the radio buttons are failing to transition from false to true upon clicking and submitting.

Below is the HTML code for the radio buttons and checkboxes:

<div>
    ... (Code here)
</div>

The properties established for the checkboxes and radio buttons are as follows:


        public bool Hex { get; set; }
        ...
        (Other boolean properties listed here)

The backend C# code responsible for assigning boolean values based on submitted data is shown below:

public async Task<IActionResult>OnPost() {
            ... (Code here)
        }

Despite trying various solutions such as altering the radio buttons and removing bootstrap classes, the issue persists. While many solutions suggest using JavaScript, I prefer to address this with C# and HTML. If anyone has insights on resolving this without resorting to checkboxes or JavaScript, any assistance would be greatly appreciated.

Answer №1

There seems to be an issue with the value attribute in your radio buttons. Each radio button should have a unique value assigned to it, corresponding to the desired boolean property in your C# backend code.

Currently, both radio buttons have a value of "", resulting in empty values for both. This could be why the values are not updating when toggling between them.

To resolve this issue, ensure each radio button has a distinct value. Here's an example:

<div class="btn-group btn-group-sm" role="group">
    <input type="radio" class="btn-check m-1" name="licenseLevel" id="SMD" value="SMD" asp-for="AddLicenseRequest.SMD" required>
    <label class="btn btn-outline-secondary m-1 mb-2" for="SMD">SMD + SM API</label>
    <span class="text-danger" asp-validation-for="AddLicenseRequest.SMD"></span>

    <input type="radio" class="btn-check m-1 " name="licenseLevel" id="SmAPI" value="SmAPI" asp-for="AddLicenseRequest.SmApi">
    <label class="btn btn-outline-secondary m-1 mb-2" for="SmAPI"> SM API Enabled</label>
    <span class="text-danger" asp-validation-for="AddLicenseRequest.SmApi"></span>
</div>

Radio buttons function as a group, allowing only one selection at a time. Different values assist in identifying the selected button and updating the relevant boolean property in the C# code accordingly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to arrange table data in Angular based on th values?

I need to organize data in a table using <th> tags for alignment purposes. Currently, I am utilizing the ng-zorro table, but standard HTML tags can also be used. The data obtained from the server (via C# web API) is structured like this: [ { ...

The placement is set to absolute, with a designated height

Here is an example http://jsfiddle.net/HnfCU/ I am using jQuery to toggle the visibility of the .child div. The position of the .child is set to absolute relative to its parent element, .parent. The challenge I am facing is adjusting the height of the .ch ...

Play framework fails to apply style attributes correctly

When working with play-framework 2.3.x, I am in the process of creating a form and would like to show/hide it based on a model attribute. Typically, setting the style attribute manually using style = "display: none" and style = "display: block" works fine. ...

The styling of HTML elements is ineffective when using scoped styles

I'm facing an issue where my element styling doesn't seem to work when using the scoped attribute. Upon inspecting the element, it appears that the styling is not being applied when using scoped. The reason I need to use scoped is because I want ...

What causes loss of focus in a React input element when it is nested inside a component?

When I have an input element connected to the React Context API, updating the value onChange works fine when the element is not nested within a component. However, if I render a different component just under the input that returns another input field conn ...

Selecting radio buttons across multiple div classes

I've been struggling to programmatically select specific radio buttons on a webpage. My goal is to automatically choose the second option in each group of radio buttons, but I'm getting lost in the syntax. Unlike most examples I've found on ...

JavaScript method to prevent users from entering numbers as the first two characters, with all subsequent characters being numbers only

I need a specific requirement for my form. The textbox field in my form is labeled "DEA License number", and I want it to only allow the user to enter alphabetic characters for the first two characters, followed by numbers afterward. I am looking to impl ...

Mobile application designed using HTML which allows for storing data offline without needing an

Looking to develop a mobile application using HTML5 that can function on both iOS devices (such as iPhones and iPads) and Android phones and tablets. The focus of the app will be on organizing grocery items, including various categories and images for eac ...

Display additional fields based on a condition in ASP.NET Core 6.0 MVC for a Yes/No input

If a user enters either Yes or No in a specific field, I want to display additional fields that need to be filled based on their choice. <div class="col-6"> <label for="Renda Bruta">Demonstrativo do Ano</label> ...

Persistent navigation once fullscreen banner is completed

I have a full screen header on my one-page website. Below the hero section is the navigation element, which I want to be fixed after scrolling past the height of the full screen. Here's what I currently have in terms of code. HTML: <div id="hero" ...

Use regular expressions and JavaScript to enclose a series of English letters within a wrapper

I am looking to enclose a series or cluster of consecutive English characters within a <span> tag. In simpler terms, I aim to alter the appearance of English language in my writing. Therefore, I need to identify English characters and surround them ...

Use JQuery to reverse the most recent button click

Here is the code snippet I am working with: <button class="btn">New York</button> <button class="btn">Amsterdam</button> <button class="btn">New Jersey</button> <button class="btn&qu ...

Methods to Maintain Consistent HTML Table Dimensions utilizing DOM

I am facing an issue with shuffling a table that contains images. The table has 4 columns and 2 rows. To shuffle the table, I use the following code: function sortTable() { // Conveniently getting the parent table let table = document.getElementById("i ...

First-character styling not applying to ID in CSS

My CSS :first-letter selector is effective on all p elements, however when applied to a span with the id #fletter, it doesn't seem to work... #fletter span:first-letter { font-weight: 600; font-size: 25px; } <span id="fletter"> The : ...

Implementing slideDown() functionality to bootstrap 4 card-body with jQuery: A step-by-step guide

Here is the unique HTML code I created for the card section: <div class="row"> <% products.forEach(function(product){ %> <div class="col-lg-3 col-md-4"> <div class="card mb-4 shadow "> &l ...

Is it possible for JSON deserialization to fail within a Controller action, yet succeed when performed explicitly?

Seeking clarification on a matter that has puzzled me. While my controller actions usually handle JSON deserialization to C# objects smoothly, there is currently an issue I am grappling with. Instead of producing the expected outcome, it presents a QBWebho ...

Showing no background color until the user lifts their finger

I am currently in the process of developing a website. The navigation on the website starts off as transparent (background: transparent) when opened, but as soon as you start scrolling, it should transition to a colorful state with shades like grey, midnig ...

I am in search of understanding the criteria for determining when an ISearchContext can be considered

I've been scouring different sources for information on this particular topic, and while I did find a similar answer here, I'm looking for a bit more detail. Here's my code: var element = myWebDriver.FindElement("User_Login"); var finalele ...

Using Jquery to create HTML elements from a C# function

I have a C# web service method that creates HTML code and returns it as a string. I want to take this HTML string from the method and replace a specific div element. function updateHTML(ID) { var gID = ID; $.ajax({ type: "get", co ...

Using Drupal to automatically adjust comment widths

My webpage features two div blocks that are set to a default width of 900px each. To make them adjust their width according to the text content, I used float: left; or display: inline-block; This caused the divs to resize based on the size of the text, b ...