Encountering issues with Html.ActionLink in Asp.net MVC when using Bootstrap Select

I am trying to implement a dropdown list for language localization on my website. However, when using <ul> tags with Html.ActionLink, the links are created with language codes as shown 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>

Unfortunately, when I try to achieve the same functionality using Bootstrap select control, it only displays text and does not redirect to the desired 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 №1

There is another alternative available for you

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

Your dropdown's change event using jQuery would appear as follows:

$('#js-data-example-ajax').on('change', function() {
        var yourUrl = $('#js-data-example-ajax').find(":selected").data('url');
        document.location = yourUrl ;
    });

Answer №2

In my project, I implemented the same functionality. Please find the code snippet below:

I utilized JavaScript for this task.

Here is my .cshtml code :

@Html.DropDownList(
    "url",
    new SelectList(new[]
    {
        new SelectListItem { Text = "About", Value = Url.Action("About", "Home") },
        new SelectListItem { Text = "MyPage", Value = Url.Action("Index", "Home") },
        new SelectListItem { Text = "APage", Value = Url.Action("AnyPage", "Home") },
    }, "Value", "Text"),
    "-- Pick an URL ---",
    new { id = "urlddl" }
)

This is my JQuery Code :

$(function() {
    $('#urlddl').change(function() {
         var url = $(this).val();
         if (url != null && url != '') {
              window.location.href = url;
         }
     });
});

By using jQuery, you can listen for changes in the dropdown list and redirect to the corresponding URL.

Hope this helps solve your issue.

Thanks

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for preventing the unwanted portrayal of a rectangle on canvas?

As a beginner in javascript, I am currently learning about the canvas. I have a question regarding drawing rectangles from right to left and seeing negative numbers in the inputs. Is there a way to display the real size of the rectangle regardless of the d ...

Running a Python script through a Javascript event

I'm looking to enhance my webpage by implementing a feature where users can generate a personalized QR code with the click of a button. My current setup involves a Python script that creates the QR code image and saves it as a .png file. I want to int ...

Issue displaying Highcharts graph within ASP.Net MVC framework

While attempting to bind data to the Highcharts library and display a column chart on a webpage, I used the following jQuery AJAX method to call the controller's action: <script type="text/javascript"> $(document).ready(function () { $.ajax ...

Looking for a way to update a world map image by selecting multiple checkboxes to apply a flood fill color to different countries using Mogrify

Exploring different methods to achieve my goal, I am wondering if creating a web service where users can track visited countries on a world map can be done in a simpler way than using Javascript or Ajax. The idea is for users to mark the countries they hav ...

Tips for utilizing Ajax in MVC 3

I am working on a form that needs to be submitted through Ajax. This form is located in a partial view, meaning the entire partial view represents a single form. To generate multiple instances of this form, I use a foreach loop: @foreach (Product p in M ...

Tips for creating a horizontally scrolling div

Here is the CSS for the div that needs to be able to scroll horizontally: .form-holder{ padding-left: 20px; width: auto; background: ; overflow-x: auto; max-height: 300px; padding-bottom: 30px; } Every ...

What is the best way to incorporate a minimum width and maximum width in CSS that add up to 100% when necessary?

Can anyone help me find CSS rules that can set a fixed width for an element (width: 500px) but also allow it to be responsive with max-width: 100% if the container is narrower than the element? I saw this example and it works perfectly: .elem { width: 60 ...

Tips on avoiding page refresh when hitting the submit button:

I am working on a form that consists of one input field and one submit button. <form method='POST' action='' enctype='multipart/form-data' id="form_search"> <input type='hidden' name="action" id="form_1" va ...

Injecting SVG styling into HTML using the content tag and CSS

I am working with 3 different files: index.html <!DOCTYPE html> <html> <head> <link type="text/css" rel="stylesheet" href="style.css" /> </head> <body> <i class="logo myRed" aria-hidden="true"></i> ...

Arranging DIVs in a vertical layout

Currently, I am working on a design that involves organizing several <DIV> elements in a vertical manner while still maintaining responsiveness. Here are some examples: Wider layout Taller layout I have tried using floats, inline-block display, ...

Utilizing Shopify API to seamlessly add items to cart without any redirection for a smoother shopping experience

Looking for a solution to submit an add to cart POST request without any redirection at all? I have tried changing return_to to "back" but that still reloads the page, which is not ideal. My goal is to smoothly add the item to the cart and notify the cli ...

The text within my div is spilling over the edges despite my attempts to use text align and overflow properties

body { font-family: Arial; font-size: 15px; } .container { position: relative; max-width: 1800px; height: auto; margin: 0 auto; text-align: center; width: 90%; } .container img { vertical-align: center; } .container .content { pos ...

How can I pass a JavaScript variable through the URL in a Rails 4 application to access it in the controller?

Struggling to pass the value of a JavaScript variable in the URL and then retrieve it in my Rails 4 app controller using param[:my_variable]. Despite trying various methods, none seem to work for me. I am unable to successfully pass my JavaScript variable ...

Looking for a Search Field that clears default text when focused - Compatible with Firefox 3.6?

I have attempted various jQuery and JavaScript solutions found on this page: How to clear text field on focus of text field Surprisingly, all of the solutions work except for FF 3.6. So, I am wondering, What alternative can I use to make this feature com ...

Checking if the current time falls within a specific range while also taking minutes into account

I am currently working on a website for restaurants offering home delivery services. I need to enable or disable the 'Order Now' button based on the designated home delivery timings of each restaurant. For this purpose, I have access to the star ...

Dynamic Column Image and Text Display

I am working on a website where I am trying to have an image on the left and a paragraph on the right, but when the screen size is reduced, I need the layout to switch so that the image is at the top and the paragraph is at the bottom. Even though I have ...

The navigation bar in responsive view on Bootstrap 4 causes the content below it to shift

Recently I have been exploring bootstrap-4 and encountered an issue while creating a nav-bar. The problem is that in responsive view, when toggling the nav-bar it simply pushes the content below it. Is there any way to address this issue within (bootstra ...

Issue with jQuery: The click event handler is not firing on elements that were previously hidden

$(document).on('click', '*', function (e) { alert('clicked'); }); I have a script that works for all elements on the current page. However, when I click an element that reveals a previously hidden div element, the click e ...

How to Effortlessly Submit a Form in a jQuery Mobile Website Using MVC

I have spent the entire day searching for a solution to this problem, hoping that it would be a straightforward task. My code successfully populates a dropdown box from a database. The dropdown is working and the form automatically submits after an articl ...

Using Angular to click and scroll to a component on a web page

Hello, I am attempting to create a basic navigation bar with a few link buttons. I would like it so that when I click on a button, it scrolls to the correct component. I am aware that this can be achieved using jQuery and ScrollTo(), but since my HTML is n ...