-1

I have a textbox and EnumDropDownList in my view.

The enum drop down list has got values

--Select--
Red
Amber
Green

Now, if I select either Red or Amber from the enum drop down, the textbox should not be blank. It can be blank if Green is chosen.

I'm completely new to MVC and jQuery.

Can this be solved using only data annotations?

If this requires jQuery how to approach it?

kunaguvarun
  • 703
  • 1
  • 10
  • 29
  • What about using a custom DropDownList implemented in a Helper method? Have a look at my answer on [Html.DropDownListFor() with custom parameter](http://stackoverflow.com/questions/8505264/html-dropdownlistfor-with-custom-parameter/20460253#). – Murat Yıldız Mar 27 '16 at 22:40
  • Use a [foolproof](http://foolproof.codeplex.com/) `[RequiredIfNot]` or similar attribute (or write your own validation attribute) –  Mar 28 '16 at 01:23

1 Answers1

0

In your View, or RazorView. I am not Going to use Html helpers, as I am not sure if you're using RazorView or not.

<input type="text" id="myTextBox" value=""/>

<select id="myselect">
    <option value="1">Red</option>
    <option value="2">Amber</option>
    <option value="3">Green</option>

</select>

Assuming you have jQuery as if you use ASP.net MVC it is there by default.

<script>
$(document).ready(function(){

  $("#myselect").on("click", function(){

        if($( "#myselect" ).val() == 3){
        $("#myTextBox").val("");
     };


    });



});

</script>
Aizen
  • 1,807
  • 2
  • 14
  • 28