dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

[Preview 8] Blazor(Server-Side) - EditForm - Non Submit Buttons are recoginsed by OnValidSubmit #13232

Closed etcircle closed 5 years ago

etcircle commented 5 years ago

Describe the bug

It feels like the buttons without type="submit" are recognized by the call OnValidSubmit. I am not sure if this is expected behavior, but here is the case.

I have a form with three buttons next to each other:

If I click on the button with "Display", I want the system to redirect me to the read-only page. Right now, it behaves as I saved it.

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core 3.0 Preview 8
  2. Run this code Edit Form with OnValidation with three different buttons

Expected behavior

I would expect that OnValidSubmit only takes the button with type=submit. Surely, there is a work-around to that by using something else that looks like a button.

<EditForm Model="@material" OnValidSubmit="Save">
        <DataAnnotationsValidator />

     <div class="form-group">
        <div class="row">
            <div>
                <a @onclick=Close class="btn-md btn-success">Close</a> 
                <a @onclick=DisplayChange class="btn-md btn-primary">Display</a>               
                <button type="submit"class="btn-md btn-warning">Save</button>
                <ValidationSummary />
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="row">
                <div class="col-md-1">
                    <label for="material">Material</label>
                </div>
                <div class="col-md-3">
                    <input @bind="material.Name" type="text" class="form-control" id="material" />
                </div>
        </div>
    </div>
    </EditForm>

private void Close()
    {
        UriHelper.NavigateTo("/materials");  
    }

    private void DisplayChange()
    {
        UriHelper.NavigateTo($"/materials/{MaterialId}/display"); 
    }

    private async void Save()
    {
         _context.Entry(material).State = EntityState.Modified;
         await _context.SaveChangesAsync();
         UriHelper.NavigateTo("/materials"); 
    }
mkArtakMSFT commented 5 years ago

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project that illustrates the problem.

pranavkm commented 5 years ago

@2nd4ce I tried this out and couldn't reproduce this using our latest nightlies. Do you have a app you could share that clearly reproduces the issue?

etcircle commented 5 years ago

Hi @pranavkm ,

Admittedly, I am relatively new to both ASP and Blazor, so I may have messed up something.

Please find enclosed the version when I had that problem. I changed the buttons since then. When you navigate to "/materials/{id}/change " and then use the "Display" button, it actually performs the "OnValidSubmit" call.

BAServer-abbaa6d1450537f781653cd2429196639d92a0b0 (1).zip

pranavkm commented 5 years ago
 <button @onclick=Close class="btn-md btn-success">Close</button>
<button @onclick=Display class="btn-md btn-primary">Display</button>

button tags without a type default to submit. This explains why you're seeing it submit. Changing it to type="button" should fix the issue. An anchor tag like you posted in the initial description should work just as well.

etcircle commented 5 years ago

Thank you! @pranavkm !