3

So I've got a dropdownlist in my View History:

@Html.DropDownList("ddlAccounts", null,  new { @onchange="onChange(this.value);" })

and I have this js event to replace the url ../History/ to ../History/2, ../History/5

function onChange(value) {
    window.location.href = value;
}

Instead it always adds the value to the current url. So I get History/2/5 for example after changing index in dropdown.

1
  • It is not required to add with '@' onchange attribute in htmlAttributes dictionary, prefix only attributes that are keywords in c# for example @class Commented Nov 28, 2012 at 19:52

1 Answer 1

2

You are not getting selected value correctly:

@onchange="onChange(this.options[this.selectedIndex].value);"

And here is structure of the current page url:

hash: "#13612872"
host: "stackoverflow.com"
hostname: "stackoverflow.com"
href: "http://stackoverflow.com/questions/13612838/asp-net-mvc4-dropdownlist-onchange-js-event/13612872#13612872"
origin: "http://stackoverflow.com"
pathname: "/questions/13612838/asp-net-mvc4-dropdownlist-onchange-js-event/13612872"
port: ""
protocol: "http:"
Sign up to request clarification or add additional context in comments.

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.