LightSwitch Look Up Lists

You have two entities Model and Model Parts, one model can have many model parts. You want to create a screen where you select one model and a list that shows only model parts for that specific model. You also have a third entity called “OT” with a relationship to the first two entities, you can select a Model and a Model Part for the OT.

model-modelpart

1. Create a Query on Model Parts call it “ModelPartsByModel” that filters only those who belong to a particular Model. The condition is Model.Id equals a new parameter call it ModelIdParameter (integer).

querymodelpart
2. In the screen you want the lookup list, click on “Add Data Item…” and select the query created in step 1.

AddDataItem
3. On the left will appear the query, click on the query’s parameter ModelIdParameter, in properties, “Parameter Binding:” select OT.Model.Id

parameter
4. On the screen item Model Part change Choices: to the query you created in step 1.

Choices

Running application:

runningapp

You can select a model, in Model Part you can select only Model Parts that belong to that particular Model.

There is an issue when you select a different model and there is a previously selected model part, it allows you to save the changes even when the model part doesn’t belong to that particular model. To address this you can use the following code in the screen code.

partial void OT_Validate(ScreenValidationResultsBuilder results)
{
    if (this.DataWorkspace.ApplicationData.Details.HasChanges)
    {
       EntityChangeSet changeSet = this.DataWorkspace.ApplicationData.Details.GetChanges();
       foreach (IEntityObject entity in changeSet.ModifiedEntities.OfType<OT>())
       {
          OT ot = (OT)entity;
          //validate that ModelPart belongs to Model
          if (ot.Modelo != null && ot.ModelPart != null)
          {
             IDataServiceQueryable<ModelPart> query;
             query = from myModelPart in this.DataWorkspace.ApplicationData.ModelPartsByModel(ot.Modelo.Id)
                     where myModelPart.Id == ot.ModelPart.Id
                     select myModelPart;

             if (query.SingleOrDefault() == null)
             {
                results.AddScreenError("Model part doesn't belong to specified model, choose another model part");
             }
          } 
       }
    }
 }

If the screen is a “New Screen” not a “Details Screen” you need to change the line:

foreach (IEntityObject entity in changeSet.ModifiedEntities.OfType())

for

foreach (IEntityObject entity in changeSet.AddedEntities.OfType())

This solution requires to put the code in every screen you need that lookup list. You could use a validation at the entity level.

I hope this help you make a lookup list in LightSwitch.

This entry was posted in LightSwitch and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s