Dave New
Dave New

Reputation: 40082

Get DropDownListFor selected value into Ajax.ActionLink call

How do I get the DropDownListFor selected value into the ActionLink route value (OrderId)?

@Html.DropDownListFor(model => model.CustomerId, new SelectList(Model.Orders, "OrderId", "Total"))

@Ajax.ActionLink("View Order", "OrderDetails", 
    new 
    { 
        OrderId = 1 // Dropdown value here!
    },
    new AjaxOptions() 
    {
        HttpMethod = "GET",
        UpdateTargetId = "OrderDetailsDiv",
        InsertionMode = InsertionMode.Replace
    })

<div id="OrderDetailsDiv"></div>

I am using MVC 5.

Upvotes: 1

Views: 3160

Answers (1)

cederlof
cederlof

Reputation: 7383

Could you put your dropdown list and link in an AjaxForm?

@using (Ajax.BeginForm("OrderDetails", new AjaxOptions { UpdateTargetId = "OrderDetailsDiv" }))
{ 
    @Html.DropDownListFor(model => model.CustomerId, new SelectList(Model.Orders, "OrderId", "Total"))
    <input type="submit" value="View Order" />
}

You would have a controller method named OrderDetails, which takes a model object, containing a property with name CustomerId.

See this answer for more information on AjaxForms and models: Using Ajax.BeginForm with ASP.NET MVC 3 Razor

For more information: http://geekswithblogs.net/blachniet/archive/2011/08/03/updating-partial-views-with-unobtrusive-ajax-in-mvc-3.aspx

Otherwise, I would do a post using JavaScript/jQuery, where you can retrieve the dropdown value, before making the request.

Upvotes: 1

Related Questions