Q:

Is there an inline class for form attributes validation in Blazor? How do you define the validations in Razor component files?

0

This code will help you to understand if there is an inline class for form attributes validation and How to define the validations in Razor component files.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

You can define the form attribute validation in a Razor component inside the function to achieve this. Please do not forget to include the DataAnnotations in the Razor component file.

@using System.ComponentModel.DataAnnotations;

<EditForm Model="@model" OnValidSubmit="@SubmitHandler">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <div class="form-group d-flex justify-content-between">
        <label class="col-form-label col-3" for="name">Name</label>
        <InputText @bind-Value="@model.Name" id="name" Class="form-control" />
    </div>
    
    <button type="submit" @onclick="@SubmitHandler" class="btn btn-primary">Submit</button>
</EditForm>
@code {
    public class ModelClass
    {
        [Required]
        public string Name { get; set; }
    }
    private ModelClass model = new ModelClass();
    private void SubmitHandler()
    {
        Console.WriteLine("Submit");
    }   
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now