C
C#•3y ago
cnetworks

How to save Checboxes values into single column?

I have a view which has a few checkboxes in mvc. i designed them in html inside view. Now I require to save the selected values of checkboxes into a single column of a table. If there are multiple checbox values, I need to save values with comma separator in that column. Please help
20 Replies
jmb
jmb•3y ago
If you have a list of your selected values you could join them e.g.
var selectedValues = new List<string>() { "1", "2", "3" };
var valuesWithCommaSeparated = string.Join(",", selectedValues );
var selectedValues = new List<string>() { "1", "2", "3" };
var valuesWithCommaSeparated = string.Join(",", selectedValues );
cnetworks
cnetworksOP•3y ago
oh nice.may I know how to get checkboxes values as list.coz I am using 6 separate checkboxes inside Create View along with other inputs. I designed in html the checkboxes. Could you guide a little more I M using a single Employess table(I'd, Name, Dept, sex, sports) in database. I m using efcore db first approach. Only one table Employee.
jmb
jmb•3y ago
Without code I cant help you a lot. But I assue you could do a onchange event listener. Which would call a function like AddToList(ChangeEventArgs cea) then you could take the value from the event argument like this
var value = (boolean)cea.Value;
var value = (boolean)cea.Value;
and then just yourList.Add(value);
cnetworks
cnetworksOP•3y ago
oh ok I will try already I wrote a onclick js fn and have it inside wwwroot/JS like just a onclick for inside submit control to catch the particular checked checkbox value and spit out alert message. But that fn not get called
jmb
jmb•3y ago
what do you use as frontend ?
cnetworks
cnetworksOP•3y ago
mvc razor with html combined
jmb
jmb•3y ago
I don't think you have to write any js for this. in razor you have attributes like @bind-value or @onchange to call the functions.
cnetworks
cnetworksOP•3y ago
oh i see razor seems tough:( checkboxes I designed in html.will that works?
jmb
jmb•3y ago
you mean in your razor page?
cnetworks
cnetworksOP•3y ago
yes yes
jmb
jmb•3y ago
yes it will work
cnetworks
cnetworksOP•3y ago
oh ok ok
jmb
jmb•3y ago
there are also ui frameworks that would make your life easier like radzen. But I would first learn how to implement this by yourself, just to understand it before using libraries
cnetworks
cnetworksOP•3y ago
oh awesome is radzen free to use?
jmb
jmb•3y ago
yes there is a community license which you can use
cnetworks
cnetworksOP•3y ago
oh ok ok.sure.but my lead asked me to do this in razor and html now.anyways I will try thanks now..right now I m home 🙂
jmb
jmb•3y ago
no problem:)
cnetworks
cnetworksOP•3y ago
thanks for your time
jmb
jmb•3y ago
no problem^^
cnetworks
cnetworksOP•3y ago
no emoji loading.so I am typing my thumb up in reply:) thumbs up @jmb friend that's fixed. Now I hv another issue. i hv created two tables and created keys Table 1: Employee Tabke(ID(pk), Name, Address) Table 2: EmployeePersonalTable( Id(fk),Phone Number, PIN) Entity First db Created keys on both tables and designed ui in view.Did code in controller. Created a view model to hold two models and tried to push it into db. However, error show up that second Table I'd can't be traced since it is not primary key. However, I created fk on I'd field.Please
Want results from more Discord servers?
Add your server