icon

CodeTrigger   Forum

Code Generation For C#, WPF, WCF, SQL SERVER/ORACLE/MYSQL and Visual Studio 2013-2017

HomeGeneral

General Discussion Board - Please post questions, answers, (polite) opinions, and use-cases. Moderators reserve the right to amend the text/subject line or re-categorise the post to better suit the forum configuration if necessary.

Binding Foreign key fields to DropDown Messages in this topic - RSS

migrator
03/12/2017
migrator
Administrator
Posts: 63
migrator
migrator
Administrator
Posts: 63
Looking at the code generated I need guidance on how to do foreign key looks For example all the generated UI screens display the primary key, none seem to support foreign key lookups, etc
0 link
03/12/2017 migrator
Administrator
Posts: 63
migrator
migrator
Administrator
Posts: 63
Hi
If I understand you correctly, you are wondering how to write a query to lookup by foreign key, potentially to bind the results to a drop down, since MVC sample generator does not provide this facility out of the box.

The page https://www.codetrigger.com/criteriaqueryapi.aspx
has plenty of examples on how to use the CriteriaQuery Api for different types of queries.

If you have the value of the foreign key then its a straightforward query

IList<BOProduct> boProducts = new Criteria<BOProduct>().Add(Expression.Eq("Category", "Fitness")).List<BOProduct>();

gives you a list of Products based on the Category foreign key: 'Fitness'

There are other examples of queries using subqueries, or selection from a table based on values in other tables etc.


If you are using MVC pattern, after code generation you would then extend the controller with some methods to run these custom queries and return data for populating the drop downs. Add methods similar to the generated methods, that take a foreign key value as parameter and return a list of values. This list you then bind to a html drop down list.

The section 'Customising Generated Code' on the page https://www.codetrigger.com/features.aspx
gives you directions on your options for customising code thats already been generated. Basically your options are either to create new files with partial classes containing the new code, or modify the generated files directly and then add them to the 'escaped files' list (Pro license version only) so that your changes do not get over-written.
edited by migrator on 12/3/2017
0 link