开发者

is it posible do have Html.ActionLink inside a DropDownList Without java script?

开发者 https://www.devze.com 2023-03-08 01:34 出处:网络
Is it possible to have something like this? @Html.DropDownListFor( @Html.ActionLink(\"About\", \"About\", \"Home\")

Is it possible to have something like this?

@Html.DropDownListFor(
                @Html.ActionLink("About", "About", "Home")
                @Html.ActionLink("mypage","index","Home")
                @Html.ActionLink("apage","anypage","Home")

                @Html.Act开发者_StackOverflow社区ionLink("Test","Test","home")
                @Html.ActionLink("pagetest", "tsetPage", "Home")
                );


No, this is not possible without javascript. Especially if you want the page to navigate to the corresponding address once the user selects an item in this dropdown. If you don't want to use javascript you could place the dropdown inside an HTML <form> but then the user will have to click on the submit button in order to navigate. Here's an example how you could achieve this with javascript:

@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" }
)

and then using jquery you could subscribe for the change event of this dropdownlist and navigate to the corresponding url:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号