Yusuf Duyar Yusuf Duyar - 1 year ago 173
ASP.NET (C#) Question

Asp.net mvc Html.ActionLink not working in Bootstrap Select

I want to create a dropdown list for my localization choice of my page. But if I tried to do this with tags Html.ActionLink helper creates true link with language code like below

var routeValues = this.ViewContext.RouteData.Values;
var controller = routeValues["controller"] as string;
var action = routeValues["action"] as string;

@Html.ActionLink("TR", @action, @controller, new { culture = "tr" }, new { rel = "alternate", hreflang = "tr" })
@Html.ActionLink("EN", @action, @controller, new { culture = "en" }, new { rel = "alternate", hreflang = "en" })

But if I want to do that in Bootstrap select control it only displays text in and not goes to my url.

<select class="bs-select form-control" data-width="auto" id="js-data-example-ajax" name="js-data-example-ajax">
<option>@Html.ActionLink("TR", @action, @controller, new {culture = "tr"}, new {rel = "alternate", hreflang = "tr"})</option>
<option>@Html.ActionLink("EN", @action, @controller, new { culture = "en" }, new { rel = "alternate", hreflang = "en" })</option>

Answer Source

you have other option like this

<option data-url="@Url.Action(@action, @controller)">

your dropdown change event in jquery look like

$('#js-data-example-ajax').on('change', function() {
        var yourUrl = $('#js-data-example-ajax').find(":selected").data('url');
        document.location = yourUrl ;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download