MADMAX
MADMAX

Reputation: 160

How can I pass selected item of select list to controller action as parameter

I want to sort data based of user selection but I am not being able to pass sleeted item to controller action.

In My View:

<form asp-action="Explore" asp-controller="Venue" method="Post">
  <div class="form-group">
    <select title="Sort by">
      <option name="name"> <a asp-controller="Venue" asp-action="Explore" asp-route-sortby="Name">Name</a> </option>
      <option name="rating">Rating</option>
      <option name="rating">Price</option>

    </select>
  </div>

In My Controller:

public async Task < IActionResult > Explore(
  string sortby) {
  return View(_context.Hotels.OrdeyrByDescending(x => x.Name).ToList();
  }

Upvotes: 1

Views: 829

Answers (1)

AbdulG
AbdulG

Reputation: 755

Assuming you are hitting the Action, add a name attribute to your <select>.

So something like this:

<select title="Sort by" name="sortby">

Bear in mind that the name must match the parameter name in the action. Also, you will need to add value attribute to all your <option> tags, like this:

<option name="rating" value="rating">Rating</option>

Upvotes: 1

Related Questions