How to get the value of “<select></select>” from a view into a Controller's “ActionResult”?

I have a tag and a button. On click of the button, an actionResult in a controller is called. Like this.

<button type="button" onclick="location.href='@Url.Action("GetAllItems", "Items")'">Get Items</button>

And here's the ActionResult

public ActionResult GetAllItems() {
        string selectedItem = string.Empty;
        ItemsRepository itemsRepository = new ItemsRepository();
        ModelState.Clear();
        return View(itemsRepository.GetAllItems());
    }

selectedItem is the variable I want to store either the selectedText or the selectedValue. But I don't know how to.

This is the <Select> tag.

<select style="width:170px" id="ItemsID" name="Items"></select>

And this is the rough function to get the different results from onChange event.

<script>
$(document).ready(function () {
    $("#ItemsID").change(function () {
        var a = $('#ItemsID option:selected').val();
        var b = $('#ItemsID option:selected').text().trim();
        var c = $('#ItemsID option:selected').index();
    alert(a);
    alert(b);
    alert(c);
})

});

Please help.

#javascript #c-sharp #jquery #asp.net

6 Likes1.90 GEEK