I have a form where I ca add and remove products, almost like a basket. But when I add/remove a product from the list, the form submits.
Is there a way to prevent this from happening?
<EditForm EditContext="FormEditContext" method="post" OnValidSubmit="SubmitForm">
<MatSelectItem
Context="Product"
@bind-Value="@formModel.Product"
TValue="TariffProductQuoteViewModel"
Items="AvailableProducts.ToList()"
Disabled="!CanEdit">
<ItemTemplate>
@Product.ProductName
</ItemTemplate>
</MatSelectItem>
<MatButton OnClick="AddProduct">+ Add Product</MatButton>
</EditForm>
public void AddProduct()
{
formModel.Items.Add(new Product()
{
ProductId = AvailableProducts.FirstOrDefault().Id,
Product = AvailableProducts.FirstOrDefault(),
Quantity = 1,
});
}
>Solution :
You have to set the Type to button on MatButton, because default value is submit (which is also true for html button).
<MatButton OnClick="AddProduct" Type="button">+ Add Product</MatButton>