Razor中的Active Select选项

时间:2014-07-15 03:01:21

标签: asp.net-mvc twitter-bootstrap razor

我正在开发我的网站的移动版本。我有选项Action工作但是当我选择时,例如“关于”它将带我到正确的页面但导航栏返回到“Home”选项。我该怎么做呢?提前谢谢

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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;
}

和其他页面。