LP13
LP13

Reputation: 34159

Disable client side validation in asp.net core at field level

In ASP.NET MVC i can disable client side validation at field level like

    @{ Html.EnableClientValidation(false); }
    @Html.TextBoxFor(m => m.BatchId, new { @class = "k-textbox" })
    @{ Html.EnableClientValidation(true); }

How do i do the same in ASP.NET Core Mvc?

I know i can do at application level in ConfigureService method

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddMvc()
        .AddViewOptions(options =>
        {
            options.HtmlHelperOptions.ClientValidationEnabled = false;
        });

}

But i want to disable for a particular field

Upvotes: 0

Views: 3524

Answers (2)

Auspex
Auspex

Reputation: 2254

This works for Asp.Net Core 2.x, and I'm pretty sure it worked for 1.0, too:

<div class="form-group">
    <label asp-for="Type" class="control-label"></label>       
    <input asp-for="Type" class="form-control" data-val="false"></label>       
    <span asp-validation-for="Type" class="text-danger"></span>
</div>

—much cleaner than the earlier code.

Upvotes: 2

Edward
Edward

Reputation: 30056

For Asp.Net Core, there is no built-in Html.EnableClientValidation(false).

For a workaround, you could try specify new { data_val = "false" } in the view like

<div class="form-group">
    <label asp-for="Type" class="control-label" validatedisable="true"></label>       
    @Html.TextBoxFor(m => m.Type, new { data_val = "false" })
    <span asp-validation-for="Type" class="text-danger"></span>
</div>

Upvotes: 1

Related Questions