Search code examples
c#asp.net-core-mvc

Conditional validation in ASP.NET Core MVC (RequiredIf)


I am trying to conditionally validate the field within ASP.NET Core MVC. I have two radio buttons. If I select Yes (for the Ownership) I want to make a field below required (Activity dropdown)

However, no matter how hard I try, the value to be validated always comes from the Activity field, not from the Ownership field ("N\A" instead of "Yes")

Can somebody please tell me what I am doing wrong

The View (chtml)

<div class=" form-group">
    <div class="bisformdynamiclabel"></div>
    <br />
    @Html.RadioButtonFor(model => model.BIS232Request.JSONData.OwnershipActivity.Ownership, "Yes", new { id = "OwnershipAnswer_true", onclick = "displayOwnershipFieldsRow(true)" })
    <label for="OwnershipAnswer_true">Yes</label>
    @Html.RadioButtonFor(model => model.BIS232Request.JSONData.OwnershipActivity.Ownership, "No", new { id = "OwnershipAnswer_false", onclick = "displayOwnershipFieldsRow(false)" })
    <label for="OwnershipAnswer_false">No</label>
    <span class="alert-danger">
        @Html.ValidationMessage("OwnershipAnswer")
    </span>
</div>
<div class="row ownershipfieldsrow">
    <div class="col-xs-12 col-md-12">
        <div class=" form-group">
            <div class="bisformdynamiclabel"></div>
            <br />
            <input style="display:none" class="form-control" type="text" asp-for="BIS232Request.JSONData.OwnershipActivity.Activity" />
            <select class="form-control ownershipactivityselect" onchange="$('#BIS232Request_JSONData_OwnershipActivity_Activity').val($(this).val());  ">
                <option value="N/A">Please Select</option>
                <option value="Manufacturer">Manufacturer</option>
                <option value="Distributor">Distributor</option>
                <option value="Exporter">Exporter</option>
                <option value="Importer">Importer</option>
                <option value="Other">Other</option>
            </select>
            <span asp-validation-for="BIS232Request.JSONData.OwnershipActivity.Activity" class="alert-danger"></span>
            <span class="alert-danger">
                @Html.ValidationMessage("OwnershipAnswerActivity")
            </span>
        </div>
    </div>

The Model

[Required]
public string Ownership { get; set; }
[RequiredIf("Ownership", "OwnershipAnswer_true", "Activity is required if Ownership is selected")]
public string Activity { get; set; }        
public class RequiredIfAttribute : ValidationAttribute
{
    private String PropertyName { get; set; }
    private String ErrorMessage { get; set; }
    private Object DesiredValue { get; set; }

    public RequiredIfAttribute(String propertyName, Object desiredvalue, String errormessage)
    {
        this.PropertyName = propertyName;
        this.DesiredValue = desiredvalue;
        this.ErrorMessage = errormessage;
    }

    protected override ValidationResult IsValid(object value, ValidationContext context)
    {
        Object instance = context.ObjectInstance;
        Type type = instance.GetType();
        Object proprtyvalue = type.GetProperty(PropertyName).GetValue(instance, null);
        if (proprtyvalue.ToString() == DesiredValue.ToString() && value == null)
        {
            return new ValidationResult(ErrorMessage);
        }
        return ValidationResult.Success;
    }
}

Solution

  • Found an answer

    Changed

    if (proprtyvalue.ToString() == DesiredValue.ToString() && value == null)
    

    to

    if (proprtyvalue.ToString() == DesiredValue.ToString() && value.ToString() == "N/A")