Click here to Skip to main content
15,896,726 members
Articles / Web Development / ASP.NET

Cascading Selection the MVC Way

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
29 Mar 2013CPOL12 min read 26K   509   25  
How to easily implement cascading selection the MVC way and leverage cached static data.
using System;
using System.Collections.Generic;
using System.Linq;
using Xomega.Framework;
using Xomega.Framework.Properties;
using Xomega.Framework.Lookup;
using System.ServiceModel;
using System.Collections;
using AdventureWorks.Client.Silverlight.EmployeeServiceRef;

namespace AdventureWorks.Client.Objects
{
    public class StateProperty : EnumIntProperty
    {
        public const string EnumTypeStates = "states";
        public const string AttributeCountry = "country";
        public const string AttributeAbbrev = "abbrev";

        public StateProperty(DataObject parent, string name)
            : base(parent, name)
        {
            EnumType = EnumTypeStates;
        }

        protected void SetLookupTable(IEnumerable<State> states)
        {
            LookupTable res = GetLookupTable();
            if (res == null)
            {
                List<Header> data = new List<Header>();
                foreach (State st in states)
                {
                    Header h = new Header(EnumType, "" + st.StateProvinceId, st.Name);
                    h[AttributeCountry] = st.CountryRegionCode;
                    h[AttributeAbbrev] = st.StateProvinceCode;
                    data.Add(h);
                }
                res = new LookupTable(EnumType, data, true);
                LookupCache cache = LookupCache.Get(CacheType);
                if (cache != null) cache.CacheLookupTable(res);
            }
            FirePropertyChange(new PropertyChangeEventArgs(PropertyChange.Items, null, null));
        }
    }

    public class EmployeeStateProperty : StateProperty
    {
        public const string EnumTypeEmpStates = "employee states";

        public EmployeeStateProperty(DataObject parent, string name)
            : base(parent, name)
        {
            EnumType = EnumTypeEmpStates;

            LookupTable res = GetLookupTable();
            if (res == null)
            {
                EmployeeServiceClient clt = new EmployeeServiceClient();
                clt.GetStatesCompleted += new EventHandler<GetStatesCompletedEventArgs>(clt_GetStatesCompleted);
                clt.GetStatesAsync();
                clt.CloseAsync();
            }
        }

        void clt_GetStatesCompleted(object sender, GetStatesCompletedEventArgs e)
        {
            SetLookupTable(e.Result);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Xomega.Net
United States United States
Xomega Team is striving to increase productivity and development quality by utilizing Model Driven Development coupled with Code Generation and the best design practices for application development.
We provide MDD tools, code generators and frameworks for Visual Studio and .Net development.
Visit us at http://www.xomega.net
This is a Organisation

1 members

Comments and Discussions