in Education by
I have an ASP.Net Core project with a simple form that has two radio buttons. One radio button has a drop-down and the other has a text-box. If the user selects the first radio button, I would like to remove/ignore the validation for the text-box (since its empty) when the user press the submit button. Vice versa, if the user selects the second option, I would like to remove/ignore the validation from the drop-down. I have tried a bunch of jQuery methods found on StackOverflow but none have work. Model: public class EnrollmentModel : BaseModel { ... public bool PriceOption { get; set; } [Required(ErrorMessage = "Please select a rate plan")] public string SelectedRatePlan { get; set; } public IEnumerable RatePlans { get; set; } [Required] public string CustomRate { get; set; } ... } } View:
@Html.RadioButtonFor(x => Model.PriceOption, true, htmlAttributes: new { @id = "comed", @class = "col-md-1" }) @Html.Label("Use price from current rate plans") @Html.DropDownListFor(x => Model.SelectedRatePlan, new SelectList(Model.RatePlans, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "rateplans", style = "width:100px;", required = "Select Rate Plan" }) @Html.ValidationMessageFor(x => Model.SelectedRatePlan, "", new { @class = "text-danger" })
@Html.RadioButtonFor(x => Model.PriceOption, false, htmlAttributes: new { @id = "custom", @class = "col-md-1" }) @Html.Label("Enter custom price") @Html.TextBoxFor(x => Model.CustomRate, htmlAttributes: new { @id = "customrate", @class = "form-control", style = "width:100px;" }) @Html.ValidationMessageFor(x => Model.CustomRate, "", new { @class = "text-danger" })
Screenshot: What is rendered: div class="form-group">
I came across something about implementing a custom validator derived from ValidationAttribute, IClientModelValidator or is there easier method to achieve what I want? Thank you. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Expressive Annotations is a library I've used several times in production projects. Just specify the conditions on your viewmodel and you're good to go. So you would have something like: using ExpressiveAnnotations.Attributes;//reference at the top public class EnrollmentModel : BaseModel { ... public bool PriceOption { get; set; } [Required(ErrorMessage = "Please select a rate plan")] public string SelectedRatePlan { get; set; } public IEnumerable RatePlans { get; set; } [RequiredIf("PriceOption == true")] public string CustomRate { get; set; } ... }

Related questions

0 votes
    I am implementing a design that uses custom styled submit-buttons. They are quite simply light grey buttons ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    List list = new ArrayList(); list.add("One"); list.add(null); list.add("Two!"); list.add ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    List list = new ArrayList(); list.add("One"); list.add(null); list.add("Two!"); list.add ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
0 votes
    The syntax for retrieving specific elements from an XML document is ______________. (1)XMLSchema (2)XQuery (3)XPath...
asked Apr 23, 2021 in Technology by JackTerrance
0 votes
    The syntax for retrieving specific elements from an XML document is __________. (1)XML Schema (2)XQuery (3)XPath...
asked Apr 22, 2021 in Technology by JackTerrance
0 votes
    I'm working on a DNN skin using Christoc's DNN templates and when I create a normal ASP.NET ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I'm building an app using Visual Studio 2010. I can run my asp.net website from my computer (by ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I am using a table to display data .I am trying to remove space from the left and right of th ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    _________ is a NiFi user who has the permit to add, remove, and modify elements in a NiFi dataflow. (1)DataFlow Manager (2)System User (3)All the options (4)DataFlow Admin...
asked Apr 20, 2021 in Technology by JackTerrance
0 votes
    _________ is a NiFi user who has the permit to add, remove, and modify elements in a NiFi dataflow. (1)DataFlow Manager (2)System User (3)All the options (4)DataFlow Admin...
asked Apr 20, 2021 in Technology by JackTerrance
0 votes
    _________ is a NiFi user who has the permit to add, remove, and modify elements in a NiFi dataflow. (1)DataFlow Manager (2)System User (3)All the options (4)DataFlow Admin...
asked Apr 19, 2021 in Technology by JackTerrance
0 votes
    How to Submit the Oracle Concurrent Requests from Backend?...
asked Dec 22, 2020 in Technology by JackTerrance
0 votes
    So the problem is i don't get get a value from textbox field on GridView RowUpdating event. Here ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I am using a user control and I need to call a popup, which asks a question, and I need to send ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
...