I'm creating grid with dynamic model structure. But i could not use ForeignKey for dropdowns.
i'am using same viewdata method for non-dynamic grid structure and its working. Because i know model name and model tree.
I want to use dropdowns with dynamic grid structure.
13 Answers, 1 is accepted
Can you send me a runable project, so I can investigate the scenario? Note that you can replace the following lines:
with hard-coded list of items in the "countries" variable, so that the example is fully runable on my side.
Hi Mr. Stamo,
I have no problem with ViewData. Its working perfectly in Kendo().Grid<Cities> and i'am creating dropdowns with ForeignKey method. Cities is known model and ForeignKey using this model and tree. I'm trying to create generic view. When i try to use Kendo().Grid<dynamic> ForeignKey don't know model tree.I could not use.
So i have problem in razor view. I want to use Grid<dynamic> instead of Grid<Cities>
Also, dynamic grid working perfectly. But no ForeignKey method. How can i use ForeignKey for dropdowns in Kendo().Grid<dynamic>
As far as I understand from the provided information, you need to use a concrete model (instead of dynamic) unless the fields that are used in the foreign key column are specified in the Model of the Schema. Currently, "ContryID" and "countryNameTr" are not defined explicitly (I cannot be sure what happens inside the loop in the model definition).
If this does not work, can you send me the project that you are testing with instead of the code snippets? Please make sure it is runable and replace the dependency that will not be available on my side, e.g.:
can be replaced with hard-coded list of items.
I have 130 tables in db. I don't want to use 130 view page for every table. I'am calling every table in one view page. This is dynamic and generic method. This is working. I have only ForeignKey problem. Yes. ForeignKey use model of the schema. I know. I'm searching solution for this. Maybe we can use different method? But how for mvc grid? This is question.
1. JsonResult Get;
2. Business Layer
3. Data Layer
This is my generic and parametric GET method... And ADD, SET, DEL working perfectly.
Get(Countries), Get(Cities), Get(Others)... Table name is parametric. I'am using same Get method for viewdata.
It is working for grid<Cities>
I investigated the case and a possible way to get the foreign key working when the grid is bound to a DataTable and dynamic model is to create a SelectList in the ViewData beforehand like this:
Then this variable can be used in the view:
Please note that you might need to make additional modifications to the column, e.g. change its template, title, etc. as this is more or less a workaround because of the dynamic configuration of the grid.
I am attaching a modified version of this example:
with the above suggestions applied.
i tried this line it gives no error,
but this line,
not working. I'm getting error "'object' does not contain a definition for 'CountryID'" becouse its needs to be in model and schema.
i tried this,
Now its working only with variable name if it is same as field name. Variable value or variable type does not matter.
Also, i tried with this "column.ColumnName" but not working. Because it requires CountryID as variable and same name with field name.
In other words, I am trying to get column.ColumnName and convert it to CountryID in a way so that I can manage to work out this line to have dynamic dropdown with ForeignKey.
The project that I sent n my previous post works with dynamic setting of the foreign key column and no error is thrown when the column and its values is set like this:
Can you have a look at it and use the same approach? Furthermore, can you confirm that you have specified the selectedValue parameter in the SelectList in the above statement:
If this is not so, it is expected for the mentioned error to be thrown as setting that is a requirement for the DropDownList to function as expected.
What I can suggest in this case (if you cannot default to "categories.First().CategoryID") is to use Reflection and get this value dynamically. This is a general approach that is not connected to the grid component, so feel free to use any other method that suits your needs (or update the current one). The following snippet demonstrates this idea:
Note that you can do the same for other properties as well. I am attaching a modified version of my previous project with the above suggestion applied.
On the other hand, it is also required to know which column(s) will be treated as foreign key column(s) beforehand in order to define them correctly, i.e. populate the DropDownList with values from the ViewData object. If they are not known beforehand, how will you determine whether a certain column should be initialized as a bound column or a foreign key column? The project that I sent relies on such a condition:
In addition, it is expected for the foreign key column to work with an ID (in this case "CategoryID"), which matches the field from the model as this field makes the foreign key relationship and is used by the DropDownList in the editor template. As I explained in my previous post, having such a dynamic structure of the grid requires certain configurations that might not be necessary in a scenario, in which the model is strongly-typed.
I have done Mr. Stamo,
its working full dynamic now.
I have been asked to implement a very similar grid, and have it about 95% of the way there, thanks to Mr. Stamo's example. The biggest difference between the provided example and mine, is that my client would like the grid to be InLine editable. I have it working to the point where the user can select a list, and it will return the correct id to the controller - but i'm seeing strange metadata that I believe to be from the select list above the actual list:
Sorry, wasn't done there. I've attached a picture of this occurring. I believe it to be related to how the SelectList i'm passing through the ViewData, somehow. If I change it to this:
..then instead of 'FalseFalse', I see '12' appear above the-dropdown. Please help - this works perfectly outside of the strange metadata above the drop down. I'll include code-snippets below:
i'am posting working code for dynamic dropdowns..
Thank you for taking the time to post your solution - i've learned that the strange metadata I was seeing in the FK column while in edit mode had to do with how I was manipulating the ViewData. I incorporated your ideas, and changed my usage of the ViewData - this fixed it completely. Thank you so much - saved me a giant headache : )