3

I want to validate some fields in blazor I am not able to do it, plz help

Error message

System.InvalidOperationException: DataAnnotationsValidator requires a cascading parameter of type EditContext. For example, you can use DataAnnotationsValidator inside an EditForm. at Microsoft.AspNetCore.Components.Forms.DataAnnotationsValidator.OnInitialized() at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

Code pic

2
  • You are using components that are designed to work within a <EditForm> not <form>. You should paste your code not an image of your code. Commented Aug 12, 2021 at 9:03
  • Hi Ashish, could you confirm if my response resolves your issue? Commented Aug 14, 2021 at 9:59

1 Answer 1

2

you need to use a code structure like this:

<EditForm Model="viewModel" OnValidSubmit="@OnValidSubmit">
                <DataAnnotationsValidator />
                <ValidationSummary />
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
<LabelText For="(() => viewModel.FirstName)" class="control-label" />
                            <InputText @bind-Value="viewModel.FirstName" id="FirstName" readonly="@viewModel.ReadonlyData" class="form-control" />
                            <ValidationMessage For="@(() => viewModel.FirstName)" />
                        </div>
                    </div>
                </div>
</EditForm>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.