How to place submit button for a Blazor EditForm outside of the component

烈酒焚心 提交于 2020-04-10 06:32:07

问题


The Blazor documentation's Form Validation example has a submit button component within the EditForm component:

<EditForm Model="@starship" OnValidSubmit="@HandleValidSubmit">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <p>
        <label for="identifier">Identifier: </label>
        <InputText id="identifier" bind-Value="@starship.Identifier" />
    </p>

    Snip....

    <button type="submit">Submit</button>

    Snip...

</EditForm>

Is there anyway to place that submit button outside of the EditForm tags and still have it 'natively' trigger the submit for that EditForm component without resorting to using JavaScript?


回答1:


A form element must have a button nested within it with its type attribute set to "submit" for the form to be submitted. I'm not sure why you want to place the submit button outside the form...

Furthermore, it is not clear why you want to resort to JavaScript ! What for ? Do you understand that the form is not really submitted to the server. Blazor is an SPA application running on the Browser (in case you use client-side Blazor), and all communication with the server is done through AJAX (HttpClient (JavaScript Fetch API)) and/or SignalR.

Actually, this is the only case in Blazor where a default action of a control is prevented (event.preventDefault=true):

const preventDefaultEvents: { [eventType: string]: boolean } = { submit: true };

function raiseEvent(event: Event, browserRendererId: number, eventHandlerId: number, eventArgs: EventForDotNet<UIEventArgs>) {
  if (preventDefaultEvents[event.type]) {
    event.preventDefault();
  }

Source: https://github.com/dotnet/aspnetcore/blob/master/src/Components/Web.JS/src/Rendering/BrowserRenderer.ts



来源:https://stackoverflow.com/questions/55975262/how-to-place-submit-button-for-a-blazor-editform-outside-of-the-component

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