2

I am migrating my MVC project to Dot net core and have been trying to fix old conditional formatting on a field that was working fine in the MVC project.

I am putting the validation on the Assumption field to be mandatory if the CategoryId is greater than 2. Below is the code I am trying.

Model

[Required(ErrorMessage = "*Required")]
public string CategoryId { get; set; }

[RequiredIf("CategoryId", Operator.GreaterThan, "2", ErrorMessage = "*Required")]
public string Assumptions { get; set; }

.cshtml View

<div class="zs-col-md-5">
Select Category 
</div>
<div class="zs-col-md-5">
    <div>
         @Html.DropDownListFor(x => x.CategoryId, new SelectList(Model.Categories, "Key", "Value", 
         Model.CategoryId), "--select--", new { @onchange = "ChangeCategoryddn(this)", @id = "category-id" })
     </div>
     @Html.ValidationMessageFor(model => model.CategoryId)
</div>
<div class="zs-col-md-5">
 Assumptions                                 
</div>
<div class="zs-col-md-5">
     @Html.TextAreaFor(model => model.Assumptions})
     @Html.ValidationMessageFor(model => model.Assumptions)
</div>

I have added the Nuget package FoolProof.Core to my project and have also referred the below javascript files:

<script src="~/lib/jquery-3.4.1.js"></script>
<script src="~/lib/jquery.validate.min.js"></script>
<script src="~/lib/jquery.validate.unobtrusive.min.js"></script>
<script src="~/lib/mvcfoolproof.unobtrusive.min.js"></script>

The validation for Category field is working fine but for Assumption (conditional field) its not working.

Is there anything that I am missing or am doing wrong.

1 Answers1

1

In Startup.cs Add in ConfigurationServices the following line

services.AddFoolProof();
Melody Magdy
  • 148
  • 3
  • 11