问题
I want to make a component that have a EditForm
and encapsulate the form and the validation inside of the component.
And I want to reuse this component anywhere in my application and submit it using any button.
How can I submit a EditForm
from a button that is outside of it?
Observation: I have searched for other answers like this one but the answer that is marked as accepted doesn't answer the question and that is why I'm making this new question.
回答1:
Instead of including the EditForm
in the component, create a component without the EditForm
and call a component's method on OnValidSubmit
<EditForm OnValidSubmit="HandleValidSubmit">
<FormContentComponent @ref="_formContent" />
<button type="submit">submit</button>
</EditForm>
@code {
private FormContentComponent _formContent;
private void HandleValidSubmit()
{
_formContent.HandleValidSubmit();
}
}
来源:https://stackoverflow.com/questions/60740060/how-to-trigger-blazors-editform-submit-from-a-button-that-is-outside-of-it