0

This has been asked and answered before, but unfortunately it doesn't work for me. How should I call a javascript function from an Html.DropDownListFor control in .Net MVC?

This is what I thought should work (on a partial view):

@model MySite.CultureViewModel

<script language="text/javascript">

    function ChangeCulture() {
        alert("please work");
    };
</script>

@using(Html.BeginForm())
{
    @Html.DropDownListFor(m => m.SelectedLanguageId, Model.LanguageSelector, new { onchange = "ChangeCulture();" })
}

What am I doing wring?

3 Answers 3

2

Your Razor code is fine. The problem is <script language="text/javascript">...

It should be <script type="text/javascript"> or <script language="javascript"> (deprecated)

Sign up to request clarification or add additional context in comments.

1 Comment

Arrggghh, how stupid am I?! Thank you so much for spotting this!
0

Don't forget extra "@":

@using(Html.BeginForm())
{
    @Html.DropDownListFor(m => m.SelectedLanguageId, Model.LanguageSelector, new { @onchange = "ChangeCulture();" })
}

Comments

0
@Html.DropDownListFor(x => x.NameID, Model.MyList, new { id = "hi" });


    $("#hi").click(function(){
    alert("Hello");
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.