Home Conditionally unobtrusive validation mode

# Conditionally unobtrusive validation mode

VansFannel
1#
VansFannel Published in 2017-09-12 10:02:11Z
 I'm developing an ASP.NET MVC 5 application with Razor, C# and .NET Framework 4.7. I want to disable some input fields on a table depending on a model's property value. I did it with javascript: if ($('#LawId').val() === "1") {$('#productsTable').attr("disabled", true); $('.addLevelButton').attr("disabled", true);$('.deleteLevelButton').attr("disabled", true); }  This works perfectly but I don't know how to disable unobtrusive validation. The razor view is: 
@Html.TextBoxFor(m => m.Products[index].Name, new { @class = "productClass" })
@Html.ValidationMessageFor(m => m.Products[index].Name)
@Html.TextBoxFor(m => m.Products[index].Description, new { @class = "productClass", @style = "max-width:none" })
@Html.ValidationMessageFor(m => m.Products[index].Description)
@Html.TextBoxFor(m => m.Products[index].Comment, new { @class = "productClass" })
@Html.ValidationMessageFor(m => m.Products[index].Comment)
@Html.CheckBox(string.Format("Delete_{0}", index))
 This is the HTML generated for the fields that I want to hide (all of them are inside productsTable table). 
 Searching I have found the helper @{ Html.EnableClientValidation(false); } but I don't know how to use it and I haven't found any complete example. I did this conditional in that Razor view to show an input file: @if (Model.LawId == 1) { }  Do I have to do the same for each field or can I do it globally for all the fields at once?
VansFannel
2#
 I've done it adding this at the beginning of the @body section: @section Body { @if (Model.LawId == 1) { Html.EnableClientValidation(false); Html.EnableUnobtrusiveJavaScript(false); } [ ... ] }