Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given the entity, I want to be able to alter the validation for Choice depending on if NotApplicable is checked.

C#
public class Answer
{
    public bool NotApplicable { get; set; }
    [Range(1, 5, ErrorMessage = "Choice must be 1-5")]
    public int Choice { get; set; }
}


Form:
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.NotApplicable,
                htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.NotApplicable)
                    @Html.ValidationMessageFor(model => model.NotApplicable,
                        "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Choice, htmlAttributes:
                new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @for (Int32 opt = 1; opt < 7; opt++)
                {
                    <div>
                        @Html.Label(opt.ToString())
                        @Html.RadioButtonFor(model => model.Choice, opt)
                    </div>
                }
                @Html.ValidationMessageFor(model => model.Choice,
                    "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}


What I have tried:

I have tried removing the validation and doing it all on the submit, but that seems a bit stupid. I would really like if the use clicks N/A then the choice is blank and disabled.
Any ideas?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900