aHaH
aHaH

Reputation: 223

How to I reference the value of a selected item in dropdownlist?

Pls look at following code

                <select name="VideoType" id="VideoType" style="width:60px">
                <option value="All">All</option>
                <option value="Movie">Movie</option>
                <option value="Show">Show</option>
            </select>

<ul id="categories">
    @foreach (var genre in Model)
    {
        <li>@Html.ActionLink(genre.Title, 
            "Browse", "Store",
              new { Genre = genre.Title, VideoType =   }, null)
        </li>
    }
</ul>

As U can see in the actionlink, how do i ref the selected value of dropdownlist? For eg: Movie.

Thanks

Upvotes: 0

Views: 568

Answers (2)

Shyju
Shyju

Reputation: 218852

Like Darin said, you do it in javascript.

Add an id attribute to your anchor tag so that we can refer using that.

@Html.ActionLink(genre.Title, 
            "Browse", "Store",
              new { Genre = genre.Title, VideoType =   }, new { @id="link1"})

Then using javascript, change the link,

$(function(){

  $("#VideoType").change(function(){
      $("#link1").attr("href")="Store/"+$("#VideoType").val();
  });

}); 

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039228

You can't do this because the ActionLink is generated on the server, whereas the selected value of the dropdown might change on the client. You will have to use javascript and subscribe to the onchange event of the dropdown and then modify the link of the anchor to include the selected value.

Upvotes: 2

Related Questions