Asp.Net MVC - Select item from a DropDownList reflect the same selection in another DropDownList that contains the same selection items

0

I have 02 DropDownList fields that have the same items to be selected, ie:

  • Camera 1
  • Camera 2
  • Camera 3
  • Camera 4
  • Camera 5
  • Camera 6

Summarizing in large part the registrations that will be carried out, selecting a DropDownList item Side A > > being any one but exemplifying  the Camera 4 item the same result of the selected item should reflect in the DropDownList Camera Side B

How to do Javascript so that I can have this field interaction, as this will prevent the user from selecting the same thing in the consecutive DropDownList.

Side A Board

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

Side B Camera

 <div class="form-group">
            @Html.LabelFor(model => model.CamaraLadoB, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("CamaraLadoB", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CamaraLadoB, "", new { @class = "text-danger" })
            </div>
        </div>
    
asked by anonymous 28.04.2018 / 04:19

2 answers

2

You can use the event change , your code would look something like this

<script type="text/javascript">     
    $("#CamaraA").change(function(){            
        $("#CamaraB").val(this.value)
    }) 
</script>

The change event is captured, after which the value of that item is assigned to the other element.

If you want to do the reverse (CamaraB change CamaraA), just replicate the code with id swapped

I left the code in .NET Fiddle for reference

    
28.04.2018 / 06:34
1

Man, if I understood the question well I already had this problem and I used select.selectedIndex with jquery and solved it. Take a look here: link

    
28.04.2018 / 06:47