Need help connecting AJAX to Controller to DAL

I kind of need help also explaining the process cause it's alot of stuff happening. But I don't know where else to turn to and my job is a kind of on the line here and my Senior dev isn't helping
55 Replies
Adrian10777
Adrian107774w ago
Pastebin
How do I connect Ajax to controller properly, then controller with ...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Adrian10777
Adrian107774w ago
easier access to code if it helps
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX4w ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
JellyRekt
JellyRekt4w ago
Is this code working but you just don't understand it? Or it's not working?
Adrian10777
Adrian107774w ago
I will work on providing a better file sorry about that guys it's not working I was able to obtain the primaryid1 when selecting dropdownlist to then push add new row to table 2 kendo button but I get a foreign key error because I don't know how to get the rest of the data to have anything if that makes sense
JellyRekt
JellyRekt4w ago
Foreign key error? Sounds to me like the table you're inserting into has a column that references another table, but the value you're inserting into that column doesn't exist as an ID in the target table.
Adrian10777
Adrian107774w ago
yeah, I assume it's because the training id I am selecting from dropdownlist, is not coming with the one that is getting the FK error because I was previously getting it for the primaryid1 but now it's a different one but if I am not mistaken I can only base my dropdownlist based on one id not multiple that's where I thought my model will come in but not sure what piece I am missing I'll be back in like an hour in case I don't respond
JellyRekt
JellyRekt4w ago
I'm confused. So sounds like you have a form to create a user, and that form contains a dropdown list of all possible training IDs. The training ID references another table, but when you go to save the user, the training ID you try to insert fails the foreign key check. Is that correct?
Adrian10777
Adrian107774w ago
So it's not a form which also confuses me, this is being done through JSON, the user is being created with the kendo button, the training id i try to insert does not fail it's another variable or scalar variable that I am not sure how I can get access too and other ones I've never done much through JSON, but in the return controller function return Json(new[] { x }.ToDataSourceResult(request, ModelState)); that's what I understand from it I guess I did a try and catch and still got : 'The INSERT statement conflicted with the FOREIGN KEY constraint
JellyRekt
JellyRekt4w ago
Okay, so a couple of things: 1. Have you placed a breakpoint on your Create method and used the debugger to make sure the column name and value are what you expect for the foreign key field?. Does the name of the key on the JSON object match the name of the column on the users table? 2. Have you verified that a record exists on the target table whose ID is the same as the value you're trying to insert in the foreign key column on your users table?
Adrian10777
Adrian107774w ago
just to be clear, I placed a breakpoint in the Create method in the controller, I say to be clear because the function name in the DAL is also Create, I can confirm the value is not what I expect, it's basically just returning 0 when it should have a value of 1 or greater, and it's the case for the other columns except for primaryid1. By name or key of JSON object do you mean x? and sorry what do you mean if it matches the name of the column on the users table (table1 or table2)? 2. Yes I verified that the ID exists in both table which are the same
JellyRekt
JellyRekt4w ago
Okay, 0 is the default value so that means it is not automatically mapping the field from the request body to the Model. If your other fields are correctly mapping to the model, my next guess is that either: 1. Your model has no default setter method for the property, so it cannot be implicitly mapped, or 2. There is no key in your request body matching the name of the property on the model; or if there is, the value's type is not one that can be mapped to the type of the property on the model.
Adrian10777
Adrian107774w ago
They all have default values except the primaryID1 which I retrieved from the dropdownlist, the ones that dont have a @scalar variable, are the only ones that should be default values problem is they can't have default values, because in the primary table they have a value, and they all are different so a default value wouldn't really work. can you explain #2 more in simpler terms sorry, not sure what you mean by key, request body, and how will I know if the value type is not one that can be mapped to the type of the property on the model?
JellyRekt
JellyRekt4w ago
Not default values, default setters. A way for the framework to set the value behind the scenes. Your properties should almost definitely be defined like this:
c#
class Model {
int ID { get; set;}
int ForeignKeyID { get; set; }
}
c#
class Model {
int ID { get; set;}
int ForeignKeyID { get; set; }
}
The { get; set; } is important because the framework will not be able to set values without that.
Adrian10777
Adrian107774w ago
I see, they all do have that and both public int
JellyRekt
JellyRekt4w ago
For #2, you say "this is being done through JSON", so I assume you are somehow sending an HTTP request to your endpoint which contains a JSON request body. The body looks something like this:
{
"key1": "value1",
"key2": "value2",
"someForeignKey": 12
}
{
"key1": "value1",
"key2": "value2",
"someForeignKey": 12
}
The keys are the part before the : and the values are the part after. Since you have a Model as your parameter, the framework will attempt to map these properties to the model. So with the body I used, it's going to look for a model definition like this:
public class Model
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public int SomeForeignKey { get; set; }
}
public class Model
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public int SomeForeignKey { get; set; }
}
The keys are used to find the property names on the model. If there's a typo in one of my keys:
{
"key1": "value1",
"key2": "value2",
"someForeignKy": 12 // misspelled "Key"
}
{
"key1": "value1",
"key2": "value2",
"someForeignKy": 12 // misspelled "Key"
}
Then it will fail to map the key to a property on the model. Since the default value of an integer is 0, SomeForeignKey will be 0 on the model; since the target table probably does not have a record with an id of 0, the FK check fails.
Adrian10777
Adrian107774w ago
Ah okay that makes sense, but how do I know if one my keys are mispelled? or where do I check for that?
JellyRekt
JellyRekt4w ago
Good question. I don't really understand how you're sending the request, so I'm not sure where the best place is to stop and examine it. If your project has HttpLogging services configured, you can probably use that to examine the request body
Adrian10777
Adrian107774w ago
no HttpLogging unfortunately, if I were to put one, do I put it in the create controller function where the Json is being returned?
JellyRekt
JellyRekt4w ago
No, that's configured wherever your DI container is configured. In Startup.cs if you have one, or Program.cs if you don't.
Adrian10777
Adrian107774w ago
have this in startup.cs public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); } } I'll look into the article and go from there, just hope it's not another rabbit hole, never did this
JellyRekt
JellyRekt4w ago
It's almost definitely a rabbithole. That's a very minimal Startup so I doubt the logging configure goes there, but I've never seen one like that before.
JellyRekt
JellyRekt4w ago
Is this what you're using to send the request? https://www.telerik.com/kendo-ui#components
Telerik.com
JavaScript UI Components - Build Better Apps Faster - Progress Kend...
Quickly build eye-catching web apps using Kendo UI's ultimate collection of JavaScript UI components with libraries for jQuery, Angular, React, and Vue.
Adrian10777
Adrian107774w ago
looks like it! they never really have anything super helpful information wise which makes it harder
JellyRekt
JellyRekt4w ago
But you do NOT have a web page you use to submit the form? I'm confused Or rather you say there is no form and it's just JSON
Adrian10777
Adrian107774w ago
no form just json, the page displays a grid of table 2, then above a dropdown, and next to dropdown a 'add row button'
JellyRekt
JellyRekt4w ago
Is it a web page in the browser?
Adrian10777
Adrian107774w ago
it is, but it's from a hospital so need admin access, and I would like to share a pic but contains sensitive info
JellyRekt
JellyRekt4w ago
No worries! If it's a web page, likely it's using the "name" property of the form control as a key in the request body. If you right click that dropdown and inspect element, you should be able to see what the "name" is in the HTML that's rendering. You'll want to verify that it matches the property on the model to which you're expecting it to map, but I don't think casing matters.
Adrian10777
Adrian107774w ago
so I am inspecting the page right now, and see <select class="form-control" data-val="true" data-val-number="The field PrimaryID1 must be a number." data-val-required="The PrimaryID1 field is required." id="Select_Staff_Dropdown" name="x.PrimaryID1 "><option value="">-- Select PrimaryID1 --</option> <option value="PrimaryID1 (it's an actual number)">name</option> ... not sure how I can verify that it matches the property on the model from here
JellyRekt
JellyRekt4w ago
See how it says name="x.PrimaryID1"? It should be PrimaryID1, no x. Right now your request body is going to look like
{
"x.PrimaryID1": <integer>,
// other properties
}
{
"x.PrimaryID1": <integer>,
// other properties
}
So there's probably a problem with how the form element is being generated. I'll have to look back at your code again
Adrian10777
Adrian107774w ago
I see, that's interesting but this ID actually goes through when going through the breakpoint
JellyRekt
JellyRekt4w ago
Interesting, I must be wrong then! I've had a similar problem before and that was how I figured out what was going on. We're looking for the key that is NOT going through, though, which is the one that is set to zero in the Model that's passed to the create action.
Adrian10777
Adrian107774w ago
yes this is what I am struggling with atm, which is why I get FK error just not sure how to solve it :/ so the model does work in my view file idk if it's something in my AJAX?
JellyRekt
JellyRekt4w ago
Your ajax looked fine to me when I glanced over it, but I'll check again
Adrian10777
Adrian107774w ago
I added this to my AJAX in case I didn't update it up there data: { grid: items, PrimaryId1: $("#Select_Staff_Dropdown").val()}, this is how the ID started working
JellyRekt
JellyRekt4w ago
What are grid and items? PrimaryID1 is the only thing working?
Adrian10777
Adrian107774w ago
I am not 100% sure tbh, I think it's what is displaying the table in the grid and yes after I added PrimaryId1: $("#Select_Staff_Dropdown").val()
JellyRekt
JellyRekt4w ago
Ah! data is the body of the request, in javascript object form. It should look like
data: {
PrimaryID1: $("Select_Staff_Dropdown").val(),
ForeignKeyID: $("foreignkey_dropdown").val(),
// Other fields
}
data: {
PrimaryID1: $("Select_Staff_Dropdown").val(),
ForeignKeyID: $("foreignkey_dropdown").val(),
// Other fields
}
Basically, for each property you're trying to add to the model, this is where you map it from the values currently in the form. I'll see if I can find an example I've written that I can show you
Adrian10777
Adrian107774w ago
one thing I realized also, is that there is a form, where the admins here can update the data, but just need to at least insert the new row for them to be able to edit it, once the row is inserted and they can see it then they should be able to edit I see, but there has to be an easier way or does it have to be each individual property? wouldn't I be able to somehow get the JSON key value pairs in there? cause that looks like key value pairs and the one I put though is an id to the dropdown
JellyRekt
JellyRekt4w ago
You mean you want a script that can automatically create the object from the values in the form without having to map them individually?
Adrian10777
Adrian107774w ago
hmm ignore the form atm, once we insert the row to the table, they should be able to edit all the other info that's connected to the db if that makes sense
JellyRekt
JellyRekt4w ago
Gotcha. In that case I would continue adding the fields you need to the data object. I'm pretty confident that will get your values all coming through to the model. When you submit a vanilla HTML form, the default handler maps the form controls automatically to a JSON object, but when you use ajax, you have to describe the body explicitly. Gonna take lunch but I'll check back in another hour or so
Adrian10777
Adrian107774w ago
how will I put it for column 13? sounds good man enjoy! I really appreciate the help I'll try and see what I get! so I added a new one that's getting the error
2 things, I have a value from the current table, but the insert statement is asking for the new value. So if current value is 3, now it needs to be 4 when inserted to db so I tried this columns13id: $(items.column13id).val() but still getting FK error
JellyRekt
JellyRekt4w ago
>2 things, I have a value from the current table, but the insert statement is asking for the new value. So if current value is 3, now it needs to be 4 when inserted to db I'm not sure what you mean here. What value do you have and what are you trying to insert?
Adrian10777
Adrian107774w ago
so the insert statement is asking for the new value the table I want to insert into, only has the new value that I am going to need not any current information so if the ID in the current table is 3, what needs to go in the insert statement is 4
JellyRekt
JellyRekt4w ago
Oh! You're trying to use incrementing IDs? You need to insert the next highest number as the primary key?
Adrian10777
Adrian107774w ago
well not for the primary key but for column 13 id for some reason
JellyRekt
JellyRekt4w ago
column13id is a reference to another table, right?
Adrian10777
Adrian107774w ago
yes but in my program the name contains new I mean in the table I am trying to add to but the primary table is the current one current one doesn't have the word new in it so like the table I am inserting into has 'new_id' but the primary table is id or cur_id if it makes more sense be back in like 30 min
JellyRekt
JellyRekt4w ago
Are you trying to insert a new record in both the main table and the table that column13id references?
Adrian10777
Adrian107774w ago
so table 1 has the primaryId1, and all the other columns that in table 2, but table 2 which is what we are inserting 2 has the word new for most of them because it's a future line, like future date, and going up a level in the medical field, so not trying to insert into table1, but table2 but I do think table2 record eventually gets into table1 in the future (but have to find out about that)
Want results from more Discord servers?
Add your server