From 1ceece1c8698ea9d0716cd5757a101cebf2e940d Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Mon, 11 May 2026 09:29:48 +0300 Subject: [PATCH] TemplateForm custom EditContext null on submit Fix #2537 --- Radzen.Blazor.Tests/TemplateFormTests.cs | 35 ++++++++++++++++++++++++ Radzen.Blazor/RadzenTemplateForm.cs | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 Radzen.Blazor.Tests/TemplateFormTests.cs diff --git a/Radzen.Blazor.Tests/TemplateFormTests.cs b/Radzen.Blazor.Tests/TemplateFormTests.cs new file mode 100644 index 000000000..a2ab34101 --- /dev/null +++ b/Radzen.Blazor.Tests/TemplateFormTests.cs @@ -0,0 +1,35 @@ +using System.ComponentModel.DataAnnotations; +using Bunit; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; +using Xunit; + +namespace Radzen.Blazor.Tests +{ + public class TemplateFormTests + { + class ContactInfo + { + [Required] + public string Name { get; set; } + } + + [Fact] + public void Submit_Passes_EditContext_Model_When_Data_Not_Set() + { + using var ctx = new TestContext(); + var model = new ContactInfo { Name = "Alice" }; + var editContext = new EditContext(model); + + ContactInfo submitted = null; + + var form = ctx.RenderComponent>(parameters => parameters + .Add(p => p.EditContext, editContext) + .Add(p => p.Submit, EventCallback.Factory.Create(this, value => submitted = value))); + + form.Find("form").Submit(); + + Assert.Same(model, submitted); + } + } +} diff --git a/Radzen.Blazor/RadzenTemplateForm.cs b/Radzen.Blazor/RadzenTemplateForm.cs index 124ecba9c..98589288d 100644 --- a/Radzen.Blazor/RadzenTemplateForm.cs +++ b/Radzen.Blazor/RadzenTemplateForm.cs @@ -181,7 +181,7 @@ namespace Radzen.Blazor if (valid) { - await Submit.InvokeAsync(Data); + await Submit.InvokeAsync(Data ?? (EditContext.Model is TItem model ? model : default)); if (Action != null && JSRuntime != null) {