blazorblazor-webassemblyblazor-editform

How does the EditForm for Blazor handle the state of the model passed to it?


So, i have a blazor EditForm and i pass a model to it.

Lets suppose i have a Person class with an Id a Name and an Age

public class Person{

    public int Id { get; set; }

    public string Name { get; set; }

    public int Age { get; set; }
}

i create an instance (Dummy) of a Person and pass it to an EditForm

<EditForm Model="Dummy" OnValidSubmit="HandleValidSubmit">
...
</EditForm>

@code{
    private void HandleValidSubmit()
    {
        //handle form submit
    }
}

so, i suppose the EditForm isnt directly modifying the original instance of the class (Dummy) because it needs to be validated first, so all the submitting logic of the (i suppose copy) of the object needs to be written by me on the HandleSubmit method, or am i missing something?

in this case how do i actually confirm the edits on the original model on the HandleSubmit method? i cant just do Dummy(Original) = Dummy(???)


Solution

  • Before the form validates the data, the handler stores all the data mapping the inputs with your model, so you just call for example:

    private void HandleValidSubmit()
    {
        var user = _userService.GetByName(Dummy.Name)
    }
    

    or simply you can use Dummy in any place, it has the data from the Form mapped