question

RussellSharp-0597 avatar image
0 Votes"
RussellSharp-0597 asked AmeliaGu-msft commented

Reflecting an option button selection in a database

I am creating an app which duplicates an existing paper-based transaction into a SQL Server database.

It is partially successful.

When I click a button to add a new record, a new record is allocated, and a form appears with no values, ready for input. When I click the "Save" button on the window after filling in data values, the values that were entered into text fields are saved to the new record in the database.

However, the form also contains a sequence of two Radio buttons. How do I reflect the Radio button selection in a boolean field within the new record? Anytime I recall a saved record, all text fields have their saved values, but none of the option Radio buttons are selected.

This question deals with the same project I previously asked about, with respect to checkboxes which exist on the form.

There are several parts to the transaction, each of which must be completed by different parties, so I have split the form app into multiple windows, each containing only the fields for that party's portion of the transaction.

Again, the record creation, retrieval, and update all work perfectly, except for storing non-text input.

Thank you very much for any help you can offer!

Below are a sample of the successful Text input, followed by my attempt at the RadioGroup:

<div class="form-cell">
<label for="comments">Comments</label><br>
@Helpers.FormField.Textarea.Enabled("comments", r1.Comments, 500)
</div>

<div class="form-cell" id="toggle_partIValidation">
<label for="PartIValidation">VALIDATION</label><br>
<div class="form-cell" id="toggle_partIValidation">
@Helpers.FormField.Radio.Enabled("PartIValidation", "V", "Validated", (r1.Part1Validation == "V" ? true : false))
@Helpers.FormField.Radio.Enabled("PartIValidation", "D", "Disapproved", (r1.Part1Validation == "D" ? true : false))
@Helpers.FormField.Radio.Enabled("PartIValidation", "R", "Returned", (r1.Part1Validation == "R" ? true : false))
</div>
</div>

sql-server-general
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RussellSharp-0597,

Can you explain more detail about the @Helpers.FormField.Radio.Enabled, it seems that it is not a standard HTML helper? Besides, about your application, whether it is a traditional Asp.net application (using .net framework) or a Asp.net core application (using asp.net core)?

Generally, when we submit the form, in the background/action method we can get the form value based on the form element's name attribute, you could check the radio button name attribute using F12 developer tool, then, in the background/action method, you can try to get the selected value based on the form element's name attribute.

Like this: Request.Form["<name>"].

0 Votes 0 ·

Hi RussellSharp-0597,

How are things going?
Did the answers help you?
Please feel free to let us know if you have any other question. If you find any post in the thread is helpful, you could kindly accept it as answer.

Best Regards,
Amelia

0 Votes 0 ·
cooldadtx avatar image
0 Votes"
cooldadtx answered

If you're using a radio button then it isn't a boolean field so you should be storing the data in your DB as either an integral or textual value. Given your code it appears you're storing it as a text value. A boolean field would only allow you 2 options and in your example you need three, hence a boolean is no longer going to work.

As for the code your expression is a little too complicated. You don't need all that.

@Helpers.FormField.Radio.Enabled("Part1Validation", "V", "Validated", r1.Part1Validation == "V");


Note that I don't recognize this as a standard HTML helper so I cannot say how it is doing its binding correctly but if you are getting the right values to save I'll assume it is working correctly. Normally we would just pass the model property to each radio button and it would figure out the button to check based upon the current value in the model property compared to the value of the radio button itself.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RussellSharp-0597 avatar image
0 Votes"
RussellSharp-0597 answered cooldadtx commented

RadioButton isn't a Boolean field? It only has two states: selected or unselected.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

A radio button's check state is boolean. But if you're using a radio button then that means you're giving the user a choice of one of several values. Since they are mutually exclusive that means you're (probably) storing the value in a single field (column in DB). Since you're using a radio button and there can be more than 2 values the DB field is not a boolean. Whether the radio button is checked or not is a boolean but that isn't what your DB field is backing, it is the value that the group of radio buttons represent.

For example suppose that you have decided to use a radio button to allow the user to choose whether they want temperature in Celsius, Fahrenheit or Kelvin. You would have 3 separate radio buttons but the actual temperature selection is a single field storing 'C', 'F' or 'K', accordingly. If you used 3 separate fields instead then a radio button is no longer appropriate as each field could be on/off without being mutually exclusive. In this case a checkbox for each field would be more appropriate.

0 Votes 0 ·