Monday 27 June 2011

My Classic Cascading Filters and Field Templates on NuGet

I’ve just released my classic Cascading Filters and Field Templates on NuGet for ease of install supports both Entity Framework and Linq to SQL.


Dynamic Data Filters and Field Templates

Sample Metadata
public partial class Vehicle
    internal sealed class VehicleMetadata
        public int Id { get; set; }
        public int ManufacturerId { get; set; }
        public int VehicleTypeId { get; set; }
        public int ModelId { get; set; }
        public int StyleId { get; set; }

        [Display(Order = 0)]
        public String Name { get; set; }

        /// <summary>
        /// Note for cascade to work correctly
        /// fields must be in cascade order Parent->Child
        /// </summary>
        [Filter(Order = 0)]
        [Display(Order = 1)]
        public Manufacturer Manufacturer { get; set; }

        [Filter(Order = 1)]
        [Display(Order = 2)]
        public VehicleType VehicleType { get; set; }

        [Filter(Order = 2)]
        [Display(Order = 3)]
        public Model Model { get; set; }

        [Filter(Order = 3)]
        [Display(Order = 4)]
        public Style Style { get; set; }

Note: For cascading to work each child must follow in order Parent->Child note the Ordering of both filters and fields to facilitate the cascade.

Next up are hierarchical cascade field templates


Yannick said...

Would this also work with for example:

- ARTNR ( PK )
- SITE ( PK & FK to SITE )

- ID ( PK )
- SITE ( PK & FK to SITE )
- Name

- SiteName ( PK )

What I'd like is when they're in the ARTICLE page that first they have to select a SITE and then they can select a supplier.

Thanks in advance,
- Yannick

Stephen J. Naughton said...

Hi Yannick, from your model no I don't think it would work, it article had SupplierId then it could work.


Graham McAnany said...

Hi Steve,

Good post. I'm interested in the Cascading Hierarchical Field Templates and downloaded your code from CascadeHierarchicalFieldTemplate - 2010-11-10a. However, if you have database tables with unique PK fields, the app falls over when trying to edit. E.g. Change Vehicle PK to VehicleId, Model PK to ModelId, etc. instead of Id for all tables.

Not sure if you've come across this already?

Stephen J. Naughton said...

Sorry I've not seen that myself sorry and I have used it extensively if many apps so far.


oterrada said...

I have the same problem of inconsistency.In 3 levels, bottom levels don't have correct top level IDs, if we change the top level id in the middle level.
Meanwhile we don't have a better solution we insert a trigger to update bottom level ids.
CREATE TRIGGER CascadingFieldUpdate_Model ON Model AFTER UPDATE AS
IF UPDATE(ManufacturerId)
declare @oldID int
declare @newID int
select @oldID = ManufacturerId from deleted
select @newID = ManufacturerId from inserted
update Style set Style_ManufacturerId = @newValue where Style_ManufacturerId = @oldValue


Stephen J. Naughton said...

Hi Uri, not sure what you mean, this is only designed to work with the type of table relationships descibed in the original post.


oterrada said...

Sorry, maybe I do something wrong.
Imagine 3 tables (table1,table2,table3) with a normalized 3 levels hier.

Table 1

Table 2

Table 3

Now, with DD in Table 3 I only see a reference to Table 2, If I need to show the cascade from Table 1, I need to add Field_Table1_ID in Table3, like:

Table 3

isn't it?

The inconsistency remains if I change Field_Table1_ID in a row on the Table 2, all elements in Table 3 that have a reference to Table2 that was changed, they have a wrong Field_Table1_ID. isn't it?

I use the trigger to fix this, when somebody change the value of Table2.Field_Table1_ID , the trigger updates the Table3.Field_Table1_ID to keep the consistency



Stephen J. Naughton said...

Hi Uri, that would work with my Hierachcal Cascade not the classic sorry.


Unknown said...


I was trying to use your cascasding filter on foreign keys in my project where I used EF 6, code first in VS 2013 and then used Scaffolding.

I am not sure if this is still relevant for the current version of EF? Most of your project demo are in 2010 and EF 4.0?

I tried to add this in and it doesn't recognize the dataannotation [Filter]?

Am I missing something or is it just not compatible in the latest incarnation of EF?

If not, would you be kind enough to direct me to a source where I can try the same...

Stephen J. Naughton said...

Filter is not a built in attribute I added it just for filter order, you would need to implement this yourself, you cann see my latest source on GitHub in my old repositories there will be a new version here as I accidentally lost access to my old one.