Writing multiple selected values into SQL Server table

I just uploaded anew version of that sample which uses a multiple select.


Here is what was needed:

  1. Retrieve all products
  2. Retrieve the order details that belong to the current order - invoke the getOrderDetails method and filter by OrderID equal to ${parameters.Id}.

    Set the $expand parameter to Product. Set the filter from the query builder
  3. Handle the Then event of the getOrderDetails method and set a new page property with Name currentProducts and Value ${result.value.map(orderDetail => orderDetail.Product)}.

Now that all needed data is in retrieve it is time to add the multiple select dropdown.

  1. Add a new form field with type lookup.
  2. Convert the Form to TemplateForm.
  3. Configure the DropDownList like this:

This will display the currentProducts page property and also update it if the user checks/unchecks items from the dropdown.

To handle the server-side updates you need to create a custom server method.

  1. Add a help class that will be used to pass the parameters - the OrderID and current products.
       public class SetProductsRequest
       {
           public Product[] Products { get; set; }
           public int OrderID { get; set; }
       }
    
  2. Inject the Entity Framework Context in the constructor of the ServerMethodsController.
       private readonly SampleContext context;
       public ServerMethodsController(SampleContext context)
       {
           this.context = context;
       }
    
  3. Add a new method that updates the database.
        [HttpPost]
        public IActionResult UpdateProducts([FromBody]SetProductsRequest request)
        {
            var order = context.Orders
                .Where(o => o.Id == request.OrderID)
                .Include(o => o.OrderDetails)
                .FirstOrDefault();
    
            if (order != null)
            {
                try
                {
                    // First remove all OrderDetails
                    foreach (var orderDetail in order.OrderDetails)
                    {
                        context.Remove(orderDetail);
                    }
                    // Then add new ones
                    foreach (var product in request.Products)
                    {
                        var orderDetail = new OrderDetail() { OrderId = request.OrderID, ProductId = product.Id };
                        context.Add(orderDetail);
                    }
    
                    // Persist changes
                    context.SaveChanges();
    
                    return Ok();
                }
                catch (Exception ex)
                {
                    return BadRequest(new { error = new { message = ex.Message } });
                }
            }
    
            return NotFound();
        }
    

The complete code is available here.

Finally invoke that method in Radzen by handling the Submit event of the TemplateForm. The request parameter is set to { orderId: ${parameters.Id}, products: ${currentProducts} }

.