Skip to content Skip to sidebar Skip to footer

Data Field Select Based On Dropdown In Asp.net Mvc

I have 2 different fields in my Database named ID1 and ID2; and have a dropdown in my View with ID1 and ID2 to select. My requirement is if the user choose ID1 in the Dropdown, the

Solution 1:

I was able to solve my problem using jquery. I added a one additional field which would hides the 2 required options. I seperated both the input fields into 2 different divisions. and then I wrote the jquery code which would carryout the condition required.

View:

<selectid="taxid"style="width:10%;"><option>Select TaxID Type</option><optionvalue="id1">ID1</option><optionvalue="id2">ID2</option></select><divid="feinfield">
                @Html.TextAreaFor(Model => Model.ID1, new { style = "width:50%; border-color: grey" })
                @Html.ValidationMessageFor(Model => Model.ID1)
            </div><divid="ssnfield">
                @Html.TextAreaFor(Model => Model.ID2, new { style = "width:50%; border-color: grey" })
                @Html.ValidationMessageFor(Model => Model.ID2)
            </div>

jquery

<script>
        $(function () {
            $('#id1field').hide();
            $('#taxid').change(function () {
                if ($('#taxid').val() == 'id1') {
                    $('#id1field').show();
                } else {
                    $('#id1field').hide();
                }
            });
        });

        $(function () {
            $('#id2field').hide();
            $('#taxid').change(function () {
                if ($('#taxid').val() == 'id2') {
                    $('#id2field').show();
                } else {
                    $('#id2field').hide();
                }
            });
        });
    </script>

Let me know if you have any concerns/queries about my solutions.

Post a Comment for "Data Field Select Based On Dropdown In Asp.net Mvc"