views:

49

answers:

2

I have a routing rule

 routes.MapRoute(
            "Search",                                                     // Route name
            "Restaurant/AdvancedSearch/{foodType}",                       // URL with parameters
            new { controller = "Restaurant", action = "AdvancedSearch", foodType = "" }  // Parameter defaults
        );

In my view I want to pass through value from a select list into the routeLink

<% using (Html.BeginForm("SimpleSearch", "Restaurant")) { %>
    <div>
        <fieldset>
            <legend>Restaurant Search</legend>
            <label for="Food">Type of food:</label>
            <%= Html.DropDownList("Food", Model.FoodType, "-- Select --")%>

            <%= Html.RouteLink("Advanced search?", "Search", new { foodType=(ViewData["Food"]) })%>
      </fieldset>
    </div>
<% } %>

Within my controller the foodType value is always null, not the item id of the selected value?

public ActionResult AdvancedSearch(int? foodType)
    {
        return View();
    }
A: 

Your RouteLink is constructed on the server side, which means that it will always be static - foodType will always be equal to the value of ViewData["Food"]. When the user changes the selection in the dropdown you need to modify your link with the new value with javascript. Here's how you could achieve this:

$(function() {
    $('select#Food').change(function() {
        $('a').attr('href', '/Restaurant/AdvancedSearch/' + $(this).val());
    });
});

Remark: this solution has a drawback: if you modify your routes it will no longer work as href is hardcoded in javascript.

Darin Dimitrov
A: 

Without seeing your model class, it's hard to know. But I think you want to do this :

<%= Html.DropDownList("Food", ViewData["Food"], "-- Select --")%>
<%= Html.RouteLink("Advanced search?", "Search",new {foodType= Model.FoodType})%>

Assuming ViewData["Food"] contains an IEnumerable<Seleclist> and Model.FootType returns the ID of the model's food type.

çağdaş