How to trigger Blazor's EditForm submit from a button that is outside of it?

非 Y 不嫁゛ 提交于 2020-03-25 17:56:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!