I need to replace ActionLink for Dropdownlist

Issue

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!

Solution

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

Answered By – Victor

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.