Asp.Net MVC - Selecting item from a Dropdownlist reflects the same selection in another Dropdownlist that contains the same selection items

Asked

Viewed 228 times

0

I have 02 Dropdownlist fields that have the same items to be selected, this is:

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

Summarizing in great part of the registrations that will be performed, the selection of an item of the Dropdownlist Camara Lado A >> being any one but exemplifying the item Camera 4 the same item result selected should reflect in the Dropdownlist Camera Side B

How to do in Javascript so I can have this field interaction, so I will avoid the user selecting the same thing in the consecutive Dropdownlist.

Camera Lado A

 <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>

Camera Lado B

 <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>

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 event is captured change, 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 the id change

I left the code on .NET Fiddle for reference

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.