Onselectionchanged MVC

Asked

Viewed 93 times

4

Late,

How can I call the Onselecionchanged event from 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

That one Bundle is already referenced in the "Master Page" _Layout.. @Scripts.Render("~/Bundles/jquery")

I did it this 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 tried that way too:

<script type="text/javascript">    
        $(function () {
            $("#ddlType").change(function () {
                if ($(this).val() == "Eletronic") {
                    $("#ddlSimilar").disabled = "disabled";
                }
            });
    });
</script>

1 answer

2


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

The correct for your case is this Bundle here:

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

I don’t 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 will give some tutorial suggestions on how you can debug Javascript written in your browser of choice:

  • Thanks for the help, I tried to use your code, but it didn’t work. I took a look at the link, and got to a function (it’s in the main topic), if you can, take a look, grateful.

  • @developer033 I updated the answer.

Browser other questions tagged

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