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) {