-3
here is my code 
<form>
    <DataAnnotationsValidator/>
    <div class=" row">
        <div class=" col-md-8 ">
            <div class=" form-group">
                <label for="Name " class="control-label">Name</label>
                <input form=" Name " class=" form-control " @bind="@objEmp.Name" />
             <Validation Message For=" (() => objEmp.Name)"/>

            </div>
</form>

 public string Name { get; set; }
        [Required]

"System.ObjectDisposedException: 'Cannot access a disposed object."

Rena
  • 30,832
  • 6
  • 37
  • 72
  • how about-> https://stackoverflow.com/questions/4573526/what-could-be-causing-a-cannot-access-a-disposed-object-error-in-wcf – Aristos Jan 21 '21 at 13:12

1 Answers1

0

Here is the sample about how to use validation in blazor:

Model:

public class ExampleModel
{
    [Required]
    public string Name { get; set; }
}

Razor Component:

@page "/"
@using BlazorApp1.Models;

<EditForm EditContext="@editContext" OnSubmit="@HandleSubmit">
    <DataAnnotationsValidator />
    <div class=" row">
        <div class=" col-md-8 ">
            <div class=" form-group">
                <label for="Name " class="control-label">Name</label>
                <input form=" Name " class=" form-control " @bind="@objEmp.Name" />
                <ValidationMessage For=" (() => objEmp.Name)" />

            </div>
            <button type="submit">Submit</button>
        </div>
        </div>
</EditForm>
@code{
    private ExampleModel objEmp = new ExampleModel();
    private EditContext editContext;

    protected override void OnInitialized()
    {
        editContext = new EditContext(objEmp);
    }
    private async Task HandleSubmit()
    {
        var isValid = editContext.Validate();
        if (isValid)
        {
            //do your stuff...
        }
        else
        {
            //do your stuff...
        }
      
    }
}

Result:

enter image description here

Reference:

https://learn.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0

Rena
  • 30,832
  • 6
  • 37
  • 72