I need to replace ActionLink for Dropdownlist

Viewed 44

I'm having a foreach that it gives me a list of values that when click it call a controller:

@foreach (var item in Model)
{
    <div>@Html.ActionLink(item.Text, "UpdateController",
                new { subSectionID = item.Value, subsectionName = item.Text })</div>
}

I would like to do this but now using a dropdown list where clicking on the value redirects me to the controller.

If someone knows how to do it or do it in some other way, maybe using Select in html, it would help me, thanks!

1 Answers

Try the following:


 <script type="text/javascript">  
    $('#subsec').change(function () {
        
        var url = $(this).val();               

        if (url != null && url != '') { 
            window.location.href = url;
        }
    });
</script>

@Html.DropDownListFor(m => Model.GetEnumerator().Current,
    Model.Select(d =>
    {
        return new SelectListItem() {
            Text = d.Text,
            Value = Url.Action("Your_Action_Name", "Your_Controller_Name",  new { subSectionID = d.Value, subsectionName = d.Text })

        };
    }),
    "-Select a value-",
    new { id = "subsec" })

A similar solution you can find here: https://stackoverflow.com/a/6088047/6630084

Related