C
C#2y ago
cnetworks

Help recquired please! I got stuck at using ui controls like dropdowns and radio buttons im .net cor

Hi, I got stuck in using ui such as Drop-down list and radio buttons for binding data from db and to push to database as well. I am confused to pick some best way to do this. Does anyone point some resources or pdfs please?
34 Replies
Binto86
Binto862y ago
No resources, but i did something like this so i can maybe help What is your scenario exactly?
cnetworks
cnetworks2y ago
thank you I hv drop down list in my mvc create view. Which gets dynamically binder from db Now it's shows redundant values. For ex if my table has 3 IT, 2 Admin, a Account items in Dept field, then my drop-down in view, fetches IT 3 times and Adm8n 2 times as it was in database.How to eliminate thus redundancy? BTW, I just using one table in database.Thank in Advance
Binto86
Binto862y ago
So u have table something like this? 1. IT 2.IT 3.IT 4.Admin 5.Admin I think you can just use SELECT DISTINCT on the db select
cnetworks
cnetworks2y ago
yes.i am scaffolding by database first..Not write a single query.entity framework core does all this. Is it I need to use a separate Department table in that same db? like a Dept table with DeptId(pk), DeptName along with main database table Employee fields( Id(pk), EmpName, DeptId(fk)) I tried this but while establishing fk,pk relation it spit error showing a message that two tables must have same number of fields.
Binto86
Binto862y ago
Um ok im not sure what are you talking about in the second part? But if you are using ef you can just juse linq for the sorting and scree on the sql
cnetworks
cnetworks2y ago
I try to explain. sorry just basic thing.i appreciate your spending of time. Let's for instance, you are doing a sample crud in .net core mvc db first approach. Name: Employee having three fields 1.Id(key) 2.EmpName 3.Department - drop-down you have to show a list of department values dynamically retriieved from db and need to binded to this control. Now, in Index view normally you list the all the data from table in a grid format. In Create view,as you know you need to show the empty view with drop-down binded with values. In Edit View, you need to retain the user's details and as you know let them to edit.Here too, I can retain the department choice in drop-down.i did that in my sample. But when users enter various inputs (records) in create view with same department, I should not repeat any of the dept items binded to the drop down. Fir that should I need to make a separate table for holding department values? like Employee table and dept table? Sorry a bit long writeup thanks many!
Binto86
Binto862y ago
Ok i hope i understand you now So if you have a set of options the user needs to chose from, than you want a separet table to store the options and reference the id of given option from main table
cnetworks
cnetworks2y ago
okay for that I tried to create a Separate Department Table with 2 fields I'd and Name and I tried to associate it's DepartmentId field to main table Employee as foreign key. Butt error display that both table must hv equal number of fields for pk and foreign key relationship. For example if the main table Employee has 6 fields,then do I need to have 6 fields in Dept table too?
Binto86
Binto862y ago
That seems weird
cnetworks
cnetworks2y ago
sorry for respond late.was off a little
Binto86
Binto862y ago
Where do u get this error from? Ef core?
cnetworks
cnetworks2y ago
no in sql server mgmt studio 2017
Binto86
Binto862y ago
Ok i never actualy set up forgein key, i always just specified it in query
cnetworks
cnetworks2y ago
but I tried creating the Dept table, after the ef scaffolding done and model generated.might be that could render error? oh i see!
Binto86
Binto862y ago
I think you can do something like put the data in dropdown, than get the id of the selected option and write jt into the DepartmentId I don't think i understand
cnetworks
cnetworks2y ago
may I know is it a good approach to create table in ql server mgmt studio or need to perform from visual studio? sorry for basic doubt
Binto86
Binto862y ago
I always just do it in the managment studio
cnetworks
cnetworks2y ago
i meant that first I created Emoloyee table and made d project in visual studio, utilized the efcore and saffolded that single Employee database from sql server. so far so good.project runs.perfect output.but I 9bserved during runtime my drop-down has values duplicates.like IT dept existed multiple times. So I later created dept table in managementstudiowith just 2 fields and tried to have the keys on those fields..Then the error displayed in sql server management studio.
Binto86
Binto862y ago
Sure i knew all of that Well nvm don't wory sbout this What if you try this?
cnetworks
cnetworks2y ago
yes sure... I think I don't understand what u are advising entirely? could you please elaborate a little?
Binto86
Binto862y ago
Ok sure So you take data from your Department table->you feed them to the dropdown->user selects one of the options->you take the option and get the id of the option -> you write that id to the DepartmentId field
cnetworks
cnetworks2y ago
oh I got it..you mean you need me to create one more field DeptId in the employee table? already I hv EmployeeId as one primary key in employee table
Binto86
Binto862y ago
Um no Bcs the forgein key doesn't really work, you can just screw on that and just do what i advised You would be writing into your already existing DepartmentId field
cnetworks
cnetworks2y ago
haha but I only hv Deptment field in Emoloyee table table which is a nvarcharp showing depth....I don't have Dept Id like..with that Dept(name) only I m using in viewbag in action methods inside Controller to move around the Department Name. Currently only EmployeeId is in my sql. I think u r advising to create separate table for Department with Id and name and suggest me to use them without creating fk right?
Binto86
Binto862y ago
Yes I thought you have that before, i guess i didint understand you corectly
cnetworks
cnetworks2y ago
oh in that case how you notify the ef that dept table is also a part of the solution without establishing pk and fk.sorry if it sounds dumb.. no I got you...you understood my point before
Binto86
Binto862y ago
Part of solution you mean like that this table exist in the db?
cnetworks
cnetworks2y ago
yes yes
Binto86
Binto862y ago
Um tbh idk, but it should be pretty easy There is going to be some command for that
cnetworks
cnetworks2y ago
i see) ok ok I will look into. Thank u so much for supporting this far. in db I can make the table inside.but in ef in visual studio, I don't know how to specify it.anyways I will refer, i am glad friend and I really appreciate ur help.thank you so much once again-
Binto86
Binto862y ago
Stack Overflow
How to update the model when using database first approach
I used EntityFramework Core database first to create model as illustrated in the EF Core documentation But I don't know how to update the model when the database has been edit.
cnetworks
cnetworks2y ago
ok sure-
Binto86
Binto862y ago
No problem Tbh xou are the most polite person i meet on this server so far
cnetworks
cnetworks2y ago
ohh thank you so much friend for your appreciation.you are so kind.To be honest, for me also, you are the most kind and lovely person who like to support and guide me in here. Your kind words made my day. i will attempt this and check!