2014-07-15 78 views
0

我正在使用我的網站的移動版本。我有選擇操作的選項,但是當我選擇時,例如「關於」它會帶我到正確的頁面,但導航欄返回到「主頁」選項。我如何去做這件事?在此先感謝剃刀中的有效選擇選項

<select class="navbar-nav" style="width:250px" onchange='location.href = this.value'> 
       <option value="@Url.Action("Index", "Home")">Home</option> 
       <option value="@Url.Action("About", "Home")">About</option> 
       <option value="@Url.Action("Products", "Home")">Products</option> 
       <option value="@Url.Action("Services", "Home")">Services</option> 
       <option value="@Url.Action("Contact", "Home")">Contact</option> 
      </select> 

回答

0
<option selected="@ViewBag.Home" value="@Url.Action("Index", "Home")">Home</option> 
    <option selected="@ViewBag.About" value="@Url.Action("About", "Home")">About</option> 
    <option selected="@ViewBag.Products" value="@Url.Action("Products", "Home")">Products</option> 
    <option selected="@ViewBag.Services" value="@Url.Action("Services", "Home")">Services</option> 
    <option selected="@ViewBag.Contact" value="@Url.Action("Contact", "Home")">Contact</option> 

,並在您Home.cshtml

@{ 
    ViewBag.Home = true; 
} 

和其他頁面。

+0

與此選項一起使用。 – user3750212

0

你可以這樣說:

@{ 
    Dictionary<string, string> menu = new Dictionary<string, string>(); 
    menu.Add("Home", @Url.Action("Index", "Home")); 
    menu.Add("About", @Url.Action("About", "Home")); 
    menu.Add("Products", @Url.Action("Products", "Home")); 
    menu.Add("Services", @Url.Action("Services", "Home")); 
    menu.Add("Contact", @Url.Action("Contact", "Home"));  

} 
<select class="navbar-nav" style="width:250px" onchange='location.href = this.value'> 
    @foreach (var m in menu) 
    { 
    <option value="@m.Value" @(m.Value == Request.Url.AbsoluteUri ? "selected='selected' " : "")>@m.Key</option> 
    } 
</select> 
+0

我試過這個,導航工作正常,但它仍然在做同樣的事情。雖然 – user3750212

+0

嘗試將空間放在「selected ='選中'」而不是。 或「selected ='true'」 –