Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7926011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:33:19+00:00 2026-06-03T18:33:19+00:00

MVC website- On the home page header section, I have a drop down with

  • 0

MVC website- On the home page header section, I have a drop down with two options in it. Each option should route to a different Controller/ActionMethod. Since I need this to be in a header section of every page, I put this in the site.master file.

Dropdown code :

      <%=Html.DropDownList("OneOrTwo", 
       new List<SelectListItem>
      {
       new SelectListItem{ Text="One", Value = "One" }, 
       new SelectListItem{ Text="Two", Value = "Two" }                       
      }
      , new { @style = "background-color:#f00; border: none;", onchange = 

       "fnNum()" }                    

      )%>

In the javascript file, I have this that routes to different controller/action method based on the drop down selection.

function fnNum()
{   
    var e = document.getElementById("OneOrTwo");
    var SelValue = e.options[e.selectedIndex].value;

    if (SelValue == "One")
        window.location.href = "Controller1/Index";  

    else
       window.location.href = "Controller2/Index";      
}

There are two issues with this approach :

  1. When selection is changed – the newly selected option does not retain. The drop down switches back to original choice even if the web page is showing the new controller/action method view…

  2. On subsequent selections of the same choice – takes me to localhost:/Controller1/Index/Controller1/Index/etc….. there has to be a clean way to do this.

So I want two things :

  1. A changed selection should be retained.
  2. On subsequent selections, the control needs to be routed properly to controller/index every time and not to controller/index/controller/index…etc..

Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:33:22+00:00Added an answer on June 3, 2026 at 6:33 pm

    To fix problem 1, you have to set the Selected property of your SelectListItems. So I would change your code to generate the drop-down to something like this:

    @Html.DropDownList("FooOrBar", new List<SelectListItem> {
            new SelectListItem{ Text="One", Value = "Two", Selected = Request.Path == "/Controller1/Index" }, 
            new SelectListItem{ Text="One", Value = "Two", Selected = Request.Path == "/Controller1/Index" }                       
        }, new { @style = "background-color:#f00; border: none;", onchange = "fnNum()" } )
    

    The second problem is easier to solve. It comes from the fact that you’re setting the window location to relative, not absolute URLs. Just change it to:

    if (SelValue == "One")
        window.location.href = "/Controller1/Index";  
    else
       window.location.href = "/Controller2/Index";  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

guy, i have a page in my asp.net mvc website. the Route configuration: public
I have a home page in JSP of my MVC application that have some
I have asp.net mvc website and in some pages i need a paging feature,
In my asp.net-mvc website I have a field that usually has a string (from
I have built a MVC website on IIS6. I used the built-in ASP.NET Security
I'm writing a MVC website and using jqGrid. I have managed to get it
I have a MVC website that sits on top of a WCF service. The
I'm building an ASP.Net MVC website. Rather than have everything in one project, I've
i have a asp.net mvc website and the volume of traffic is increasing. I
All, I have a PHP website written in Zend Framework and MVC. Most of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.