Search code examples
visual-studio-lightswitchlightswitch-2013

Lightswitch HTML - adding data dynamically


I have this bit of code:

// Add a new Customer
var newOrderLine = new myapp.Part();
// Set the Status

newOrderLine.UnitPrice = 0;
newOrderLine.Equipment.Order.OrderID = 641;

// Save all changes on the screen
return myapp.activeDataWorkspace.ApplicationData
    .saveChanges().then(function () {
        // Refresh the Customers
        screen.getParts();
    });

now this works for adding in any field that does not contain a relationship, however the problem i am now having is trying to associate the foreign key to its root source (OrderID) as alls that occurs is the data is inserted into the database, but the OrderID remains null.

Tables:

  • Orders
  • Parts (1 to many - many Parts can feature on one Order)

and thanks to this link for the help http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/1195/Dynamically-Creating-Records-In-The-LightSwitch-HTML-Client.aspx


Solution

  • If you look further down that article in the section Creating The Complex Example, it shows you how to do precisely what I think you're asking. Note that the .set[EntityName] method takes an argument of an entity (newOrderLine.Equipment.Order in your case) and not just the OrderId.

    Phil