Reflecting an option button selection in a database

Russell Sharp 1 Reputation point
2021-08-19T14:47:00.053+00:00

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 Other
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2021-08-19T15:02:05.447+00:00

    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.

    0 comments No comments

  2. Russell Sharp 1 Reputation point
    2021-08-19T15:11:19.587+00:00

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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.