Skip to content
Advertisement

Get ID and Value from a checkbox: Return to model and use in method to insert into sql database

I’m trying to create a checkbox list where a user is supposed to be able to choose one or more options, based on the choice: this is supposed to be inserted to a database table, where the id of the choice is inserted. (This is on a page where a user can “edit garage”), therefore the garageid is also supposed to be fetched and both the garageid and the choice id should be inserted to a cross table, that I have created as following:

JavaScript

I also have a stored procedure for the insert:

JavaScript

And the “edit page” is working and functioning, it’s where I get the garageId as well. It looks like following in view:

JavaScript

Model:

JavaScript

Method:

JavaScript

If you look at my comments in the model and the method, you can see what I’ve added for the “choices” function I’m trying to implement. Here’s the html i created for the input type as well:

JavaScript

Now, to the problems:

1: I need some sort of method for making sure which or if a checkbox is checked, and this needs to be returned to the model or controller in some way. I only want to return it’s numeric value, as seen in the model I want Tires to have the numeric value of 2 etcetera.

  1. The datebase insert works (so at least that’s something), but the table only accepts RequestProperty and GarageID, meaning that if a user chooses 2 of the checkboxes, I need to update the database twice, creating 2 rows, but with the same garageid.

I tried posting a question about this earlier, but it was poorly explained from my side, so I’m trying again, and hoping I included everything this time. I’m open to any help/solutions that can help me solve this.

Advertisement

Answer

First, You need to remove all GarageCrossRequestType that containe current GarageID as the checkbox may be checked and unhacked later on edit.

This how I would do it.

Note: make sure to read the comment

javascript

JavaScript

C#

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement