Yusuf Duyar Yusuf Duyar - 2 months ago 17
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;
}

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


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>
</select>

Answer

you have other option like this

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

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 ;
    });
Comments