Home Conditionally unobtrusive validation mode
Reply: 1

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:

<td>
    <div class="group">
        @Html.TextBoxFor(m => m.Products[index].Name, new { @class = "productClass" })<br />
        <div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Name)</div>
    </div>
</td>
<td>
    <div class="group">
        @Html.TextBoxFor(m => m.Products[index].Description, new { @class = "productClass", @style = "max-width:none" })<br />
        <div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Description)</div>
    </div>
</td>
<td>
    <div class="group">
        @Html.TextBoxFor(m => m.Products[index].Comment, new { @class = "productClass" })<br />
        <div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Comment)</div>
    </div>
</td>
<td>
    <div class="group">
        @Html.CheckBox(string.Format("Delete_{0}", index))
    </div>
</td>

This is the HTML generated for the fields that I want to hide (all of them are inside productsTable table).

<div class="group">
    <input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Products_0__Id" name="Products[0].Id" type="hidden" value="0" />
    <input data-val="true" data-val-number="The field Law must be a number." data-val-required="The Law field is required." id="Products_0__Law" name="Products[0].Law" type="hidden" value="1" />
    <input data-val="true" data-val-length="Must be 14 characters long" data-val-length-max="14" data-val-length-min="14" data-val-required="Product&#39;s code is requiered" id="Products_0__ProductCode" name="Products[0].ProductCode" type="number" value="" />
    <div class="mensajeError"><span class="field-validation-valid" data-valmsg-for="Products[0].ProductCode" data-valmsg-replace="true"></span></div>
</div>

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)
{
    <input name = "ChinaCodes" id = "ChinaCodes" class="upload" type="file">
}

Do I have to do the same for each field or can I do it globally for all the fields at once?

VansFannel
2#
VansFannel Reply to 2017-09-12 10:19:09Z

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);
    }

    [ ... ]
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.400913 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO