OnSelectionChanged MVC

4

Late,

How can I do to call the OnSelectionChanged event of a DDL using MVC?

I have the following DDL:

@Html.DropDownListFor(model => model.Type, ViewBag.Type as SelectList, "-- Select --", new { id = "ddlType", onchange = "onchange()" })

And based on the item selected in this DDL, I give a Hidden in another DDL, that is, this 2nd DDL will only be shown if a specific option of the 1st DDL is checked.

The 2nd DDL is this:

@Html.DropDownListFor(model => model.SimilarId, ViewBag.SimilarId as SelectList, "-- Select --", new { id = "ddlSimilarId" })

PS: I also wanted to do this for a RadioButton ..

Thank you in advance.

EDIT

This Bundle is already referenced in the "Master Page" _Layout ..     @ Scripts.Render ("~ / bundles / jquery")

I did it that way, nothing happens, what is still missing?

@section Scripts {
   @Scripts.Render("~/bundles/jqueryval")
}

<script type="text/javascript">

    $(function onchange() {

        if (document.getElementById("ddlType").value == "Eletronic") {
            document.getElementById("ddlSimilarId").disabled = "disabled";
        }
    });
</script>

I've tried it too:

<script type="text/javascript">    
        $(function () {
            $("#ddlType").change(function () {
                if ($(this).val() == "Eletronic") {
                    $("#ddlSimilar").disabled = "disabled";
                }
            });
    });
</script>
    
asked by anonymous 20.05.2015 / 20:34

1 answer

2

First of all, I think it's worth knowing how jQuery works .

The correct one for your case is this Bundle here:

@Scripts.Render("~/bundles/jquery")

I do not know what your logic is, but a start would be something like this:

$(document).ready(function() {
    $("#idDaDropDownList").change(function() {
       if ($(this).val() == "Algum valor") {
           // Coloque aqui a lógica
       }
    });

    $("#idDaDropDownList").trigger('change');
});

As I do not know your code, I'll give you some tutorial suggestions on how you can debug JavaScript written in your browser of choice:

20.05.2015 / 23:04