TemplateForm custom EditContext null on submit

Fix #2537
This commit is contained in:
Vladimir Enchev
2026-05-11 09:29:48 +03:00
parent fef48efd78
commit 1ceece1c86
2 changed files with 36 additions and 1 deletions

View File

@@ -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<RadzenTemplateForm<ContactInfo>>(parameters => parameters
.Add(p => p.EditContext, editContext)
.Add(p => p.Submit, EventCallback.Factory.Create<ContactInfo>(this, value => submitted = value)));
form.Find("form").Submit();
Assert.Same(model, submitted);
}
}
}

View File

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