.NET Editor: Relational Dropdowns to db entry
.NET Editor: Relational Dropdowns to db entry
Hello Allan,
is it possible to add relational dropdown options, depending on some conditions of the current iterating database entry?
Here is an example:
Table 'Cars' structure: CarId, CarName, CarColorId
1 Car1 1
2 Car2 3
Table 'Colors' structure: ColorId, ColorName
1 Red
2 Yellow
3 Blue
Linked Table 'CarsColors' (possible colors for a car) structure: CarId, ColorId
1 1
1 2
2 2
2 3
Inside datatables I want to show a dropdown for Car1 with the content Red and Yellow and for Car2 another dropdown with the content Yellow and Blue.
So I would create my db query like this:
...
var response = new Editor(db, "Cars", "CarId")
.Field(new Field("Cars.CarName"))
.Field(new Field("Cars.CarColorId")
.Options(*1)) //Problematic
.Process(Request.Form)
.Data();
...
*1 and here is my problem:
In the manual are two ways described to configure the Options(). I could add string parameters to connect to the 'Colors' table and take the 'ColorId' as value and 'ColorName' as label. As fourth parameter I could add a condition but there is no reference to i.e. the cars table, so I can set it dependent on the "current" car. The second described way is the closure function, but I think I also dont have a useful reference to the "current" context there.
Do you understand my problem and is there a way to solve this with the current version?
This question has an accepted answers - jump to answer
Answers
Hi,
Thanks for your question. The answer for your question here is partly client-side and partly server-side. Since the server can't know in advance which row(s) the user will choose to edit, we need to use a client-side construct to see which row they edit. That can then ping to server to see what options should be shown based on the selected row's data.
The way we do that in Editor is with the
dependent()
method. Basically you add a dependency to the "Cars.CarName" field (client-side) with a URL that will ping the server with a request to get the data for the that row. The server should then respond with data in the format described in thedependent()
documentation that will define the options for the colour field.Regards,
Allan
Thank you for this!
But I can not set the initial options with this, right? So when I load my page, the table loads with the ouput "CarName" and "ColorName" and when I click on the edit button, I get my edit lightbox. The CarName is displayed there but whats with the select dropdown for the colors?
I think I would need something that triggers when opening the edit lightbox or when I inline edit an item. Then I have to send a request and update the dropdowns. But you are right, I also need it when changing the CarName.
i.e. editor.on( 'open', function ( e, mode, action ) { /* trigger dependent */ }); + an event for inline edit
By the way: I am trying to receive the .dependent() ajax request in my .NET action but I cant bind the sent data to a model. Is there a ready to use model in your .dll or how can I correctly bind the data?
Without being able to see the page, I'm afraid I can't say. However, I presume that it is empty. If the colour is dependent on the car, then the colour list can't be populated until the car is selected (or have I misunderstood)?
Because the object submitted can potentially vary significantly between implementations, there is no standard model for this. You would need to read the HTTP POST parameters directly from the request or otherwise parse it into a modal if you need that.
Allan
If I click "new" and add a new entry, you are right. Then I first have to choose a car and then it can add the available colors to the color dropdown. But what if I want to edit an existing car? So when the lightbox opens when I click "edit", the car dropdown is filled with i.e. car2 and the color dropdown should be filled without first changing the car dropdown again.
So the color dropdown should not be updated when changing the car dropdown (thats what happening when registering.dependent() to the car dropdown, if I am right) but when opening the lightbox.
Correct. The
dependent()
call should be triggering an Ajax call when the form enters edit mode to get the colour options for the car that is being edited, and select the colour from the first for the current value.If that isn't working for you, I'd need a link to the page to be able to debug the code and understand what is going wrong.
Allan
Ok my fault, I missunderstood it. I thought its like a normal event listener, that is simply listening to 'on change' of the dropdown. Thank you Allan!
Now I just have to bind the sent data to a data structure. I tried to bind 'values' to 'object', 'string[]', 'ICollection<string>' but none are working.
Ok, I found a solution now.
I.e. I have a JoinModel with nested classes and the dependet method sends data to my action:
I had to create a new model i.e. StaffDependent.cs where the property types are the single classes of my JoinModel and the names are also the class names.
Here I had to modifiy the property name, because it may not be exactly (case sensitive) like the type. So I can not name it i.e. users but Users. But the model binder can bind it without problems, because it binds without case sensitive.
And in my dependent action:
Fantastic to hear - thanks for posting back!
Allan