8bitboy
8bitboy

Reputation: 63

ASP.NET MVC4 + dropdownlist + onChange js event

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.

Upvotes: 3

Views: 6828

Answers (1)

karaxuna
karaxuna

Reputation: 26940

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:"

Upvotes: 2

Related Questions