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

谁说胖子不能爱 提交于 2019-12-21 21:51:09

问题


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.ActionLink("Test","Test","home")
                @Html.ActionLink("pagetest", "tsetPage", "Home")
                );

回答1:


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;
        }
    });
});


来源:https://stackoverflow.com/questions/6088042/is-it-posible-do-have-html-actionlink-inside-a-dropdownlist-without-java-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!