is it posible do have Html.ActionLink inside a Dro

2019-04-15 14:48发布

问题:

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