Adding multiple related records at once

Hi!

I'm very new to Razen, but I already do love it a lot. I started with some basic things and got most of what I need already working.

However, there is one thing I'm uncertain about how I can implement this:

I have a table "Customer" and table "Contact" where a customer has one contact record (using the ContactID FK linked to the ID of the "Contact" table.

When I create a new page with a data grid and populate it with "Customer"(s) I can use ${expand} to also include the fields of the "Contact" table.

Now, when I add a button to create a new customer record, how can I at the same time also create a new contact record which is then already linked to the newly created customer record?

I was only able to get it working in a way that I can assign an existing contact record to the new customer record but I want to manage the fields form customer and contact I one page and give the end user the impression as if all the displayed fields belong to the customer record.

Any ideas what needs to be done to get a scenario like this running?

Best regards,

Joe

Hi @JustJoe,

The only option I can suggest is to create your own custom method:

Hello @enchev,

thanks for your reply. So you mean that the "Add" button executes some custom C# code in the OnClick handler which creates a new Contact object and and new Customer object and links both together? This is actually the same way as I did in a former LightSwitch project from which I am migrating. Is there an example somewhere how and where to write this custom code? Any example the I can derive from as a starting point?

Thanks a lot and best regards,

Joe

I've just noticed that I've posted wrong link. Here is the correct one:

Perfect. I Think I almost got it working. Here es the code that I call when the "Add" button is clicked:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Radzen;
using Radzen.Blazor;
using Microsoft.AspNetCore.Components;
using RadzenTestApp.Data;

namespace RadzenTestApp.Pages
{
    public partial class MyCustomerComponent
    {
        [Inject]
        LocalSqlServerContext localSqlServerContext { get; set; }

        public Models.LocalSqlServer.Customer CreateNewCustomerRecord()
        {
            var newCustomer = new Models.LocalSqlServer.Customer();
            var newContact = new Models.LocalSqlServer.Contact();
            newContact.Company = "New Company";
            newCustomer.Contact = newContact;

            localSqlServerContext.Customers.Add(newCustomer);

             return newCustomer;
        }
    }
}

And in the Click handler I have:

1.) Invoke custom method and then Set proptery customer to ${result}.

When I click the button, the new record is created and the form is popuplated with the data, but when I click on "save", I get an error message "Error - unable to update customer".

I'm not sure if I forgot something essential here. Any ideas?

Thanks so much for your excellent and quick support!

Best regards,

Joe

You can run the application with Visual Studio to check the actual error.

That is what I already did. But is just shows:

Ausnahme ausgelöst: "System.Exception" in RadzenTestApp.dll
Ausnahme ausgelöst: "System.Exception" in System.Private.CoreLib.dll

No further details..

I set VS to also step into handled exceptions and got the point where the error happens:

"Item no longer available" in

public async Task<Models.LocalSqlServer.Customer> UpdateCustomer(int? id, Models.LocalSqlServer.Customer customer)

So there must be a way to tell Radzen that the new record has been set in the UI.

Thanks, I finally got it working. My code was correct so far, but I had to take some contraints into account when creating the new record.

One final quesition: In the orignal code for adding a new record, there is also a property "isEdit" set to "false" after the new record has been created. Is this required or what it this used for?

Thanks so much!

This is used in the default templates and not needed in your case.